java为安卓 ListView中的不同行提供不同的背景色
我的Android应用程序屏幕上有一个列表视图。在列表视图中,我显示了来自ArrayList的数据。 现在Arraylist有3个字段:Id、Name和Status。 我需要在屏幕上显示Id和名称,并根据状态值,可以是0、1或2设置特定行的背景色。我可以用屏幕上的值获取ListView,但我似乎找不到任何可以在create上设置安卓列表视图中行颜色的示例。有人能帮我吗?现在我使用SimpleAdaptor来显示列表的值。提前感谢您。:-)
这是我的ListView xml::
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="wrap_content"
安卓:id="@+id/relativeLayout"
安卓:layout_height="wrap_content"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:orientation="vertical"
安卓:background="#FFEBEB"
tools:context=".MyActivity2">
<ListView
安卓:id="@+id/listView"
安卓:layout_width="wrap_content"
安卓:layout_height="fill_parent"
安卓:layout_centerInParent="true"
安卓:choiceMode="singleChoice"
安卓:textAlignment="center"/>
</RelativeLayout>
这是添加行的XML::
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:paddingTop="4dip"
安卓:paddingBottom="6dip"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:layout_gravity="left">
<TextView 安卓:id="@+id/TITLE_CELL"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:gravity="left"
安卓:layout_weight="1"
安卓:height="40dp"
安卓:textAlignment="center"
安卓:padding="10dp"/>
<TextView 安卓:id="@+id/FROM_CELL"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:gravity="right"
安卓:height="40dp"
安卓:textAlignment="center"
安卓:padding="10dp"/>
</LinearLayout>
# 1 楼答案
如果需要一些随机颜色代码,请尝试以下操作:
# 2 楼答案
您可以在
Adapter class getView()
方法中执行此操作。例如:# 3 楼答案
我相信您将需要一个自定义适配器。重写
getView()
方法,并根据需要设置视图背景