1

有谁知道如何以编程方式从字符串数组中设置列表项的背景?我有两个字符串数组,一个是文本视图的标题,另一个包含颜色参考。我已将标题数组添加到数组适配器中,并且正在显示,但现在我想更改数组中每个项目的背景颜色

这是我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
      >

    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="18dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center" />

</RelativeLayout> 

继承人我的代码到目前为止

     ListView menuList = (ListView) findViewById(R.id.listView1);
        String[] items = {"menuItem1","menuItem2","menuItem3","menuItem4","menuItem4","menuItem5","menuItem6","menuItem7","menuItem8"};  
  String[] colors = {"#ffffff","#000000","#ffffBB","#ffffDD","#ff654d","#ffffff","#ffffff","#ffffff","#ffffff"};   
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1, items);
        menuList.setAdapter(adapter);
4

3 回答 3

6

使用自定义适配器,如下所示:

public class MyAdapter extends ArrayAdapter<String> {

    Context context; 
    int layoutResourceId;    
    String data[] = null;
    String color[] = null;

    public MyAdapter(Context context, int layoutResourceId, String[] data, String[] color) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        this.color = color;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        StringHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new StringHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.text1);

            row.setTag(holder);
        }
        else
        {
            holder = (StringHolder)row.getTag();
        }

        holder.txtTitle.setText(data[position]);
        row.setBackgroundColor(Color.parseColor(color[position]));

        return row;
    }

    static class StringHolder
    {
        TextView txtTitle;
    }
}

listView然后设置如下的适配器:

ArrayAdapter<String> adapter = new MyAdapter(this, R.layout.drawer_list_item, items, colors);
menuList.setAdapter(adapter);

更新刚刚注意到:您还需要更新布局文件。它似乎错误地定义了 textview 的 id。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="50dp"
        >

    <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="18dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:gravity="center" />

</RelativeLayout> 

结果如下:

在此处输入图像描述

于 2013-10-29T11:56:53.457 回答
0

这就是我为 2 种颜色所做的。

我在 color.xml 中保留了 2 个颜色字符串,并在列表视图的自定义适配器中使用了它

if (position % 2 == 0)

            convertView.setBackgroundResource(R.color.color1);
        else
            convertView.setBackgroundResource(R.color.color2);
于 2013-10-29T11:48:46.563 回答
0

您可以使用自定义阵列适配器。根据参数扩展ArrayAdapter和覆盖getView以返回带有背景颜色的文本视图position

于 2013-10-29T11:49:21.030 回答