更改简单ListView的文本颜色多项选择Android [英] Change text color of simple ListView Multiple choice Android
问题描述
我正在为板球开发一个应用程序.我的要求是这样的,如果我选择第1队,则必须显示可用国家/地区名称的列表,如果我选择一个国家/地区名称作为印度,则必须显示来自印度的球员列表,并且我要从中选择多个球员.我已经做了一切.但是我的问题是我正在使用android.R.layout.simple_list_item_multiple_choice选择玩家.我正在使用简单的列表视图,该列表的背景是黑色图像.我的列表视图就是这样
I am developing an app for cricket. My requirement is like this, if i select team 1 the list of available country name has to display and if i select a country name as India the list of player from India has to displayed and in that i have select a multiple players from that. I have done everything. But my problem is i am using android.R.layout.simple_list_item_multiple_choice for selecting players. I am using simple list view and the background of that list is black image. And my listview is like that
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8.5"
android:cacheColorHint="#00000000"
/>
现在问题是listview值显示为黑色.我已经有黑色背景图像.而且该值也为黑色.所以它看起来不太好.如何在不更改自定义适配器的情况下将listview值的颜色更改为白色.
Now the problem is the listview value is showing black in color. Already i have black background image. And the value also black in color. So its not looking good. How to change the color of listview values to white without changing o custom adapter.
这是我的适配器类
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,playersName);
lvview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lvview.setAdapter(adapter);
推荐答案
您必须创建Custome TextView
来更改所有ListView items
的颜色,而不是将默认的android.R.layout.simple_list_item_multiple_choice
传递给ArrayAdapter
,您应该传递自定义列表项XML,它具有不同的TextColor属性.
You have to create Custome TextView
to change color of all ListView items
, instead of passing default android.R.layout.simple_list_item_multiple_choice
to ArrayAdapter
you should pass custom list item XML, which has different TextColor attribute.
例如,在文件夹布局"下创建了custom_list_item.xml:
For example, created custom_list_item.xml under folder Layout:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF00FF"
/>
然后将其传递给适配器,如下所示:
Then passed it to adapter like as below:
new ArrayAdapter<String>(this, R.layout.custom_list_item, playersName);
已
这是我测试过的可以正常工作的代码.
Here is the code which is working fine i have tested.
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_list_item, playersName));
lv.setBackgroundColor(Color.BLACK);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> p_arg0, View p_arg1, int p_arg2, long p_arg3)
{
my_sel_items = new String("Selected Items");
SparseBooleanArray a = lv.getCheckedItemPositions();
for (int i = 0; i < a.size(); i++) {
if (a.valueAt(i)) {
my_sel_items = my_sel_items + ","
+ (String) lv.getAdapter().getItem(i);
}
}
Log.v("values", my_sel_items);
}
});
列表视图的布局
<ListView
android:id="@+id/android:list"
android:layout_marginTop="60dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
/>
这篇关于更改简单ListView的文本颜色多项选择Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!