Android:如何创建类似于 gmail 应用程序中的列表背景?

标签 android

在附上的 Gmail 应用截图中,ListView 的背景似乎在列表的右侧有一个垂直渐变,以与相邻的 View 分开。

我知道我可以在 xml 中定义一个 GradientDrawable,但我如何使用它来绘制渐变作为列表一侧的背景,同时将列表的其余部分的背景绘制为浅灰色?

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#DDDDDD"
      android:endColor="#CCCCCC"
      android:angle="0" />
</shape>

enter image description here

最佳答案

您只需将创建的 cutsom 可绘制对象指定为 ListView 项目的背景属性。您可以在用于定义列表元素的 XML 布局中执行此操作。

例如,我使用此布局 (list_item.xml) 为我的列表项定义自定义布局(并将渐变指定为背景):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_item_gray_gradient"
    android:orientation="horizontal"

...

关于Android:如何创建类似于 gmail 应用程序中的列表背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14362658/

相关文章:

android - 将 BroadcastReceiver Intent 发送到服务

android - 获取权限拒绝异常

javascript - 即使使用 setJavascriptEnabled(true) 和 WebChromeClient,Android webview 也会跳过 javascript

android - Delphi Xe8 从右到左支持

android - 如果我的代码使用 RTTI 那么它不能在 android 上运行?

java - Android Collection.sort 根据 Android API 版本不一致地崩溃

android - 尽管我使用的是 DP,但分辨率较低的手机上的 UI 尺寸较大?

java - 您好,我尝试将数据库中的字符串值转换为 int,但没有发生

java - Android:动画自定义 View

java - 打开应用程序和服务器之间的连接