首页 文章

单击该项目时如何更改项目列表视图的文本视图颜色?

提问于
浏览
0

我有一个ListView,每个项目都有一个TextView .

我想更改项目TextView颜色,例如,当用户单击该项目时 .

任何想法,例子或教程?

谢谢

2 回答

  • 2

    使用list of states .

    例如,在drawable文件夹中创建一个名为bg_states.xml的文件:

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
       android:drawable="@color/blue" ></item>
    <item
        android:state_pressed="true"
       android:drawable="@color/blue" ></item>
    <item 
        android:drawable="@color/black" /> 
    </selector>
    

    并将其定义为TextView的背景:

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/bg_states"
    
  • 0

    尝试一下

    v.setBackgroundColor(R.Color.Red);
    

    onClick 方法中

相关问题