我正在使用数组适配器创建一个 ListView 。这是我的代码。
listView = new ListView(context);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_single_choice, choice);
listView.setAdapter(adapter);
这为文本提供了黑色。我需要将文本颜色更改为蓝色。我们怎样才能改变这一点。我将我的布局与阵列适配器一起使用。这是代码
listView = new ListView(context);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(context,layout.my_single_choice, choice);
listView.setAdapter(adapter);
下面给出了 my_single_choice.xml 的代码
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/my_choice_radio"
android:layout_height="match_parent"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="Option"
style="@style/ListItemTextColor" />
这不起作用,我可以选择我所有的单选按钮。当 clickListener 不工作时,...?我们该如何解决呢
最佳答案
可以通过一些方法来使用它。最常见的方法如下:
- 使用自定义 ListView ( http://www.androidpeople.com/android-custom-listview-tutorial-part-1 )
使用自定义布局(不在 android.R 命名空间内)
在你看来,使用这段代码:
<?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"/>
在你的类里面,使用这段代码:
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);
我最喜欢的,重写 ArrayAdapter 的 getView 方法
ListView listView = (ListView) this.findViewById(R.id.listView); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations() ) { @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = (TextView) super.getView(position, convertView, parent); String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location); int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark; textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor)); return textView; } });
此外,这是此处讨论的结果:How to change text color of simple list item
关于android - 带有 simple_list_item_single_choice 的文本颜色数组适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16686413/