我正在尝试更改Listview的文字颜色。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, stg1);
我使用默认XML来调用数据列表,现在要求更改此列表视图的文本颜色。我知道我可以使用其他自定义类,但是还有其他方法可以通过仅使用此类来更改文本颜色。
我搜索了很多并尝试了很多其他解决方案,但每个人都建议使用自定义类,但我不想使用自定义类。
以下是我所提及的链接。
How to change the list view text color?
Android: Change text color in ListView for singleChoice
how to change the color of the text of the default ListView in android?
How to Change List View Text color
Ans还有很多其他链接,但是每个人都建议使用自定义类,采用文本视图并更改文本视图的颜色,但我不想使用任何其他XML文件或任何自定义类。
即使我不知道这是否可能,所以请帮助我。提前谢谢。
答案 0 :(得分:3)
步骤1):创建XML布局文件custom_list_item_multiple_choice
:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textColor="#FF0000"
/>
其中,android:textColor="#FF0000"
指定您的文字颜色。
步骤2):按照以下方式启动适配器:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.custom_list_item_multiple_choice, stg1);
其他一切都保持不变..一切都应该正常。
答案 1 :(得分:0)
尝试
自定义适配器textview 中的 textView.setTextColor(Color.rgb(0,102, 51));
如果您没有使用自定义适配器,请使用ArrayAdapter<Spanned>()
为文本制作自定义颜色。
答案 2 :(得分:0)
这可能对你有帮助......
ArrayList<String> arrayList = new ArrayList<String>();
// add your String items to array list
ArrayList<Spanned> spannedList = new ArrayList<Spanned>(
arrayList.size());
for (int i = 0, N = arrayList.size(); i < N; i++) {
String s = arrayList.get(i);
String html = "<font color=\"#0000FF\">" + s + "</font>"; // blue color
Spanned spanned = Html.fromHtml(html);
spannedList.add(spanned);
}
ArrayAdapter<Spanned> adapter = new ArrayAdapter<Spanned>(
this, android.R.layout.simple_list_item_1, spannedList);
您可以使用颜色代码将文本设置为任何颜色。 (这里我使用蓝色0000FF)。有关更多颜色代码,请参阅here
答案 3 :(得分:0)
您可以使用
Html.fromHtml()
在使用之前,您需要迭代适配器值并将该值存储在一个跨区变量中,如
Spanned out=Html.fromHtml("here you can use html tags like font color ,heading tag etc");