如何更改 ListView 中文本的颜色?

How to change color of text inside ListView?

如何在我的代码中更改 ListView 文本(在抽屉布局中)的颜色? 在我的代码中,列表视图采用各种其他布局。 我尝试了 Whosebug 中可用的各种其他代码,但 none 似乎对我有用

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/lltoolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:orientation="vertical">

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar" />

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lltoolbar"
        android:text="@string/hello_world" />


</RelativeLayout>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_marginTop="55dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </FrameLayout>

    <ListView
        android:id="@+id/drawerList"
        android:entries="@array/abcd"
        android:background="#FFFFFF"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left" />

</android.support.v4.widget.DrawerLayout>


private Toolbar toolbar;
TextView textView;
ImageView iv;
DrawerLayout drawerLayout;
private ListView listView;
private String[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    textView = (TextView) findViewById(R.id.toolbar_title);
    iv = (ImageView) findViewById(R.id.ivClick);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);       

    listView = (ListView) findViewById(R.id.drawerList);
    names = getResources().getStringArray(R.array.abcd);
    listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names));
    listView.setOnItemClickListener(this);

}

按照以下步骤操作:

  1. 创建一个 xml 布局,它将显示在列表视图的每一行中。 (目前您正在使用默认的 android.R.layout.simple_list-item_1 布局)像这样:
    my_listrow_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sample text"
        android:text_color="#006699"
        android:id="@+id/list_item"/>
</LinearLayout>

2. in your activity code, change your list adapter code to this:

listView.setAdapter(new ArrayAdapter<>(this, R.layout.my_listrow_layout,
R.id.list_item, names));

my_listrow_layout 是您为列表视图定制的布局,R.id.list_item 是您的文本视图在列表视图布局中的 ID。要更改文本的颜色,请在您刚刚创建的布局中添加 android:text_color="your desired color" 值。 (我已经将它设置为#006699,深蓝色)。

您应该为列表视图的每一行创建一个 xml 布局,然后创建一个 class 扩展 BaseAdapter 并在 getview 内部膨胀 xml 布局,然后为文本视图设置颜色