Android ListView中不同行的不同背景颜色

时间:2014-09-10 08:27:06

标签: java android android-listview

我的Android应用屏幕上有一个ListView。在列表视图中,我显示了ArrayList中的数据。 现在,Arraylist有3个字段:Id,Name和Status。 我需要在屏幕上显示Id和Name,并根据状态值,可以是0,1或2设置特定行的背景颜色。我能够使用屏幕上的值获取ListView,但我似乎无法找到任何可以在create列表视图中设置行颜色的示例。有人可以帮帮我吗?我现在使用SimpleAdaptor来显示列表的值。先感谢您。 : - )

这是我的ListView xml ::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:id="@+id/relativeLayout"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#FFEBEB"
tools:context=".MyActivity2">

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:choiceMode="singleChoice"
    android:textAlignment="center"/>

</RelativeLayout>

这是添加rows ::

的XML
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="4dip"
    android:paddingBottom="6dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="left">


    <TextView android:id="@+id/TITLE_CELL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_weight="1"
        android:height="40dp"
        android:textAlignment="center"
        android:padding="10dp"/>

    <TextView android:id="@+id/FROM_CELL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:height="40dp"
        android:textAlignment="center"
        android:padding="10dp"/>

</LinearLayout>

3 个答案:

答案 0 :(得分:4)

您可以使用Adapter class getView()方法执行此操作。例如:

@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {       
        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.list_row, parent, false);
        }

        //do your stuff

        if(status.equals(0))
        {
          convertView.setBackgroundColor(Color.RED); // or whatever you want to set color
        }
        else if(status.equals(1))
        {
          convertView.setBackgroundColor(Color.GREEN);
        }
        if(status.equals(2))
        {
          convertView.setBackgroundColor(Color.YELLOW);
        }       

        //do your remaining stuff....

        return convertView;
    } 

答案 1 :(得分:3)

如果你想要一些随机颜色代码,试试这个:

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{       
    if(convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_row, parent, false);
    }

    Random rnd = new Random(); 
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    convertView.setBackgroundColor(color);

    return convertView;
} 

答案 2 :(得分:0)

我相信你需要一个自定义适配器。覆盖getView()方法,并根据需要设置视图背景。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder;
if (convertView == null) {
 // inflate the layout

            convertView = inflater.inflate(R.layout.adapter, parent, false);

            // well set up the ViewHolder
            viewHolder = new ViewHolder(convertView);

            // store the holder with the view.
            convertView.setTag(viewHolder);

        }
        else {
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Item myItem = getItem(position);
        int id = myItem.getMyItemId();
         switch (id) {
            case 1:
            convertView.setBackgroundResource("whatever");
            break;
            case 2:
            convertView.setBackgroundResource("whatever ever");
            break;
            case 3:
            convertView.setBackgroundResource("whatever not");
            break;
            default:
            break;
        return convertView;
}