gpt4 book ai didi

Android从字符串数组设置listItem背景颜色

转载 作者:行者123 更新时间:2023-11-29 14:50:30 24 4
gpt4 key购买 nike

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

这是我的布局

<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);

最佳答案

按如下方式使用自定义适配器:

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>

结果如下:

enter image description here

关于Android从字符串数组设置listItem背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19657303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com