如何更改简单列表项的文本颜色 [英] How to change text color of simple list item
问题描述
我有一个 ListActivity 并且我正在显示一个列表:
I have an ListActivity and i am displaying one list with:
setListAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_single_choice, mStringList));
默认列表项的文本颜色为白色,我想将列表中文本视图的文本颜色更改为黑色.
By default text color of list items is white, I want to change text color of text views in the list to black.
我该怎么做?
推荐答案
另一种最简单的方法是创建一个布局文件,其中包含你想要的 textView 以及你喜欢的 textSize、textStyle、color 等,然后将它与 ArrayAdapter 一起使用.
Another simplest way is to create a layout file containing the textview you want with textSize, textStyle, color etc preferred by you and then use it with the ArrayAdapter.
例如mytextview.xml
e.g. mytextview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/font_content"
android:padding="5sp"
android:layout_width="fill_parent"
android:background="@drawable/rectgrad"
android:singleLine="true"
android:gravity="center"
android:layout_height="fill_parent"/>
然后像往常一样将它与您的 ArrayAdapter 一起使用
and then use it with your ArrayAdapter as usual like
ListView lst = new ListView(context);
String[] arr = {"Item 1","Item 2"};
ArrayAdapter<String> ad = new ArrayAdapter<String>(context,R.layout.mytextview,arr);
lst.setAdapter(ad);
这样您就无需为其创建自定义适配器.
This way you won't need to create a custom adapter for it.
这篇关于如何更改简单列表项的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!