【问题标题】:How to change the list view text color?如何更改列表视图文本颜色?
【发布时间】:2015-10-22 01:37:12
【问题描述】:

有人可以帮我在不使用任何基本适配器的情况下更改列表视图的文本颜色吗?我使用 android.R.layout.simple_list_item_single_choice 来显示选项按钮,但默认情况下,列表视图中的文本以白色显示,我需要将其更改为黑色。有没有办法在不使用自定义适配器的情况下将文本更改为黑色。我已经粘贴了我用来创建带有选项按钮的列表视图的代码。

  ListView lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
            save = (ImageView)findViewById(R.id.button1);
            lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            lvCheckBox.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, values));

【问题讨论】:

    标签: android listview


    【解决方案1】:

    只需更改 ListView 项的默认布局

    而不是:

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_listview_item, list);
    

    使用

    adapter = new ArrayAdapter<String>(this, R.layout.customLayout, list);
    

    和customLayout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="20dp" />
    

    【讨论】:

    • 嗨 arichie,我试过你的回答,我明白了。但是选项按钮已经消失了。我也需要选项按钮。您还有其他建议吗?
    • 选项按钮是指右侧的单选按钮??
    • 是的,抱歉,我的意思是单选按钮。
    • 我认为你应该使用 RadioButton 而不是 TextView。我不确定这在没有任何自定义适配器的情况下是否有效
    【解决方案2】:

    使用此代码。

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.custom_spinner_textview_layout, from);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerAlarmTone.setAdapter(adapter);
    

    Xml 布局文件

    < ?xml version="1.0" encoding="utf-8"?>
    <TextView 
      xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16.0sp" 
        android:textColor="@color/black"
        android:gravity="left"/>
    

    【讨论】:

    • 这是出现在微调器还是列表视图中
    • 但我需要它在列表视图中。
    • 你有没有用我的代码用 listview 而不是 spinner 检查它?
    【解决方案3】:

    代替默认的android.R.layout.simple_list_item_single_choice,您可以使用由文本视图组成的自定义xml 文件并将此xml 文件传递​​给ArrayAdapter。通过这种方式,您可以轻松更改 textview 颜色。但我仍然建议您使用自定义适配器以获得更大的灵活性。

    【讨论】:

      【解决方案4】:

      是的!你是对的,我试过你的代码。 默认情况下,它也显示白色背景和 Textcolor。当我们按下它时,由于选择器颜色而显示文本。所以它对我们来说是可见的。我接受上述答案。但我们只能在特定部分进行定制。无需创建单独的 Layout 或 Textview。

      ListView lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
                      save = (ImageView)findViewById(R.id.button1);
          lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      
          lvCheckBox.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, values) {
                  @Override
                  public View getView(int position, View convertView, ViewGroup parent) {
                      CheckedTextView checkedTxtView = (CheckedTextView) super.getView(position, convertView, parent);
      
                      String yourValue = values.get(position);
                      checkedTxtView.setText(yourValue);
                      checkedTxtView.setTextColor(this.getResources().getColor(android.R.color.black));
                      return textView;
                  }
      
          });
      

      我已经创建了示例 hello world 项目并尝试了这个。这个对我有用。我希望它能帮助你完成你的任务。

      还有一件事,您需要 CheckedTextView,因此您使用的是 android.R.layout.simple_list_item_single_choice。如果您按照上述示例创建自定义布局,则必须在这些自定义布局设计中使用 CheckedTextView。因为,在上面的例子中,他们只使用了简单的文本视图。所以请注意这一点。

      【讨论】:

        【解决方案5】:

        您可以使用自定义选择器,例如,用于列表视图:

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"    +
         android:drawable="@drawable/listitem_pressed" />
        <item android:state_focused="true" android:drawable="@drawable/listitem_selected" />
         </selector>
        

        参考: Another Ask

        【讨论】:

          【解决方案6】:

          兄弟的一种解决方案是您将列表视图的背景颜色更改为黑色,您的文本将自动可见。 或者您必须在列表视图上设置自定义适配器。 当您为自定义布局充气时,您可以根据需要设置 textview 颜色。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多