有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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>

共 (3) 个答案

  1. # 1 楼答案

    如果需要一些随机颜色代码,请尝试以下操作:

    @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. # 2 楼答案

    您可以在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;
        } 
    
  3. # 3 楼答案

    我相信您将需要一个自定义适配器。重写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;
    }