【问题标题】:Change ListView's textcolor更改 ListView 的 textcolor
【发布时间】:2011-07-30 16:53:52
【问题描述】:

我正在尝试更改 ListView 的文本 color,但我找不到如何做到这一点..

    listViewObject= (ListView) findViewById(R.id.chathlist);        
    ArrayList<String> chatHistory = new ArrayList<String>();
    chatHistory.add("Msg 1");
    chatHistory.add("Msg 2");
    chatHistory.add("Msg 3");
    chatHistory.add("Msg 4");
    chatHistory.add("Msg 5");
    chatHistory.add("Msg 6");
    chatHistory.add("Msg 7");
    ArrayAdapter aa=new ArrayAdapter<String>(getApplicationContext(),
    android.R.layout.simple_list_item_1, chatHistory);
    listViewObject.setAdapter(aa);
    listViewObject.invalidate();

我尝试了很多方法,但我无法更改颜色。这可能是一件简单的事情,但我正在努力解决这个问题。

我的 XML 文件是

  <RelativeLayout android:id="@+id/chat_history_container"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
            <ListView android:paddingTop="15dip" android:paddingBottom="15dip"
                android:layout_width="fill_parent" android:paddingLeft="15dip"
                android:layout_height="wrap_content" android:id="@+id/chathlist"
                android:layout_gravity="center_horizontal"
                android:layout_centerInParent="true" android:paddingRight="15dip"
                 android:textColor="#FF0000"  android:listSelector="@drawable/gradient_box" >
            </ListView>
  </RelativeLayout>

【问题讨论】:

    标签: android listview colors android-widget


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      问题是您使用的是默认列表项布局“android.R.layout.simple_list_item_1”。如果要更改列表视图中文本的颜色,您必须使用正确的文本颜色创建自己的列表项布局。

      您可以将此项目添加到您的布局文件夹中:

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/text1"
              style="?android:attr/listItemFirstLineStyle"
              android:paddingTop="2dip"
              android:paddingBottom="3dip"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" 
          android:textColor="#FF0000" /> 
      

      然后传递这个item而不是android.R.layout.simple_list_item_1

      【讨论】:

      • 如果您的列表视图设置为 singleChoice 以显示 RadioButtons,这当然是无用的。编写自己的自定义布局需要开发自己的适配器和更多代码。如果所有列表都只显示没有 RadioButtons 的文本项,则您的解决方案很好。
      猜你喜欢
      • 2016-11-08
      • 1970-01-01
      • 2011-05-01
      • 2020-06-29
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多