android - 带有 simple_list_item_single_choice 的文本颜色数组适配器

标签 android android-listview radio-button android-arrayadapter

我正在使用数组适配器创建一个 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/

相关文章:

css - 验证 HTML 中的复选框

java - AlertDialog 不显示在 android studio 中

android - 在 Kotlin 中处理事件总线的通用监听器

android - android动态添加数据到ListView

android - 当 ListView 中的 View 值更新时,适配器未更新

android - 根据列表android中的值更改listview中列表项的颜色

javascript - 有两个表单,其中包含相同的单选按钮组吗?

android - android 如何知道应用程序是否安装成功

android - android 时间值超过 60 分钟

iphone - 在 Tableview 中添加单选按钮遇到一些问题