在附上的 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>
最佳答案
您只需将创建的 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/