【问题标题】:Different background color for different rows in an android ListViewandroid ListView中不同行的不同背景颜色
【发布时间】:2014-09-10 08:27:05
【问题描述】:

我的 Android 应用屏幕上有一个 ListView。在列表视图中,我显示来自 ArrayList 的数据。 现在 Arraylist 有 3 个字段:Id、Name 和 Status。 我需要在屏幕上显示 Id 和 Name 并根据状态值(可以是 0、1 或 2)设置特定行的背景颜色。我能够使用屏幕上的值获取 ListView,但我似乎找不到任何可以在创建时在 android 列表视图中设置行颜色的示例。有人可以帮帮我吗?我现在使用 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>

这是添加行的 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>

【问题讨论】:

  • 您必须创建自定义适配器并在您的适配器中设置视图。然后检查您的状态值并设置背景颜色。
  • 你能在你的适配器中显示你的getView的代码吗?
  • 你应该使用 BaseAdapter 来自定义列表视图,见例子:stackoverflow.com/questions/16333754/…
  • 您好,感谢您的所有回答。我想我应该提到这一点,我知道如何使用定制适配器来做到这一点,正如我所提到的,我只是使用 SimpleAdaptor 并想知道是否有任何可能的方法来仅使用自定义适配器?谢谢大家的回复,都是正确的,希望能全部采纳。

标签: java android android-listview


【解决方案1】:

您可以在 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;
    } 

【讨论】:

    【解决方案2】:

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

    @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;
    } 
    

    【讨论】:

      【解决方案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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-06
        • 2012-02-23
        • 1970-01-01
        • 1970-01-01
        • 2021-08-26
        • 1970-01-01
        • 2017-06-21
        • 1970-01-01
        相关资源
        最近更新 更多