[英]how to change the color of the list items in list view
我在Activity类中使用ListView。 我没有为ListView使用任何XML代码。 现在,我无法更改listView中Text项的颜色。
我更改了ListView的背景颜色,但无法更改列表项的颜色。 我从互联网上获得了一些链接,但无法弄清楚。 有人可以帮我做一些代码吗?
我的Activity类如下所示:
ListView listView;
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "India", "Malaysia" };
TextView tv = new TextView(getApplicationContext());
tv.setText("Select Country");
tv.setTextColor(012);
listView = getListView();
listView.addHeaderView(tv);
listView.setCacheColorHint(Color.rgb(36, 33, 32));
listView.setBackgroundColor(Color.rgb(225, 243, 253));
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));
}
在上面,为列表项的标题设置了tv.setTextColor
。 它不起作用,因为它要求我传递一个整数值。 我可以为Color传递什么整数值? 谁能建议一些代码来更改列表项的颜色?
要使列表项具有彩色,需要对其进行自定义。为此,请准备一个xml文件:
custom_listitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:id="@+id/list_item"
android:background="#FF0000" <!-- for red color -->
/>
</LinearLayout>
现在,您必须像在适配器中那样使用它-
listView.setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_item,R.id.list_item,names));
是的,您可以使用Color.RED
, Color.YELLOW
等作为默认颜色,我们可以将"#3C3C3C"
(在xml中使用)或Color.parseColor("3C3C3C")
(以编程方式使用)用于任何默认颜色默认颜色以外的其他颜色。
您需要创建一个CustomListAdapter。
private class CustomListAdapter extends ArrayAdapter {
private Context mContext;
private int id;
private List <String>items ;
public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
{
super(context, textViewResourceId, list);
mContext = context;
id = textViewResourceId;
items = list ;
}
@Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
TextView text = (TextView) mView.findViewById(R.id.textView);
if(items.get(position) != null )
{
text.setTextColor(Color.WHITE);
text.setText(items.get(position));
text.setBackgroundColor(Color.RED);
int color = Color.argb( 200, 255, 64, 64 );
text.setBackgroundColor( color );
}
return mView;
}
}
列表项如下所示(custom_list.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>
使用TextView api装饰您喜欢的文本
你会像这样使用它
listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.