【问题标题】:text Color ArrayAdapter with simple_list_item_single_choice带有 simple_list_item_single_choice 的文本颜色 ArrayAdapter
【发布时间】:2013-05-17 04:31:54
【问题描述】:

我正在使用数组适配器创建一个 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 不工作时,...?我们该如何解决它

【问题讨论】:

  • 你试过 android:textColor 吗?
  • 您应该为您的列表视图使用自定义适配器。我认为您可能需要复选框而不是单选按钮。stackoverflow.com/questions/16685366/…。并在 checkbox.xml 中添加 @blackbelt 告知的文本颜色

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


【解决方案1】:

有一些方法可以使用它。最常见的方法如下:

  • 使用自定义列表视图 (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

【讨论】:

    【解决方案2】:

    如果您真的想保持简单并且不想添加任何新的布局或文件,您可以执行以下操作来更改 simple_list_items 的颜色。

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, listItems) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView = (TextView) super.getView(position, convertView, parent);
                // Set the color here
                textView.setTextColor(Color.parseColor("#000000"));
                return textView;
            }
        };
    listView.setAdapter(adapter);
    

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      相关资源
      最近更新 更多