【问题标题】:How to change color of text inside ListView?如何更改 ListView 中文本的颜色?
【发布时间】:2015-12-26 13:37:29
【问题描述】:

如何在我的代码中更改 ListView 文本(在抽屉布局中)的颜色? 在我的代码中,listview 处于其他各种布局中。 我已经尝试过 stackoverflow 中可用的各种其他代码,但似乎没有一个对我有用

<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);

}

【问题讨论】:

    标签: android android-listview navigation-drawer


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      按照以下步骤操作:

      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。在您的活动代码中,将您的列表适配器代码更改为:

      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 深蓝色)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 2013-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多