ListView 客製化-文字顏色

摘要:ListView 客製化-文字顏色

 

1.在drawable 文件下新建itemcolor_black.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:state_pressed="true" android:color="@color/White"/> //非觸碰模式字體顏色
    <!-- pressed -->
    <item android:state_focused="true" android:color="@color/White"/> //焦點並點擊時的字體顏色
    <!-- focused -->
    <item android:state_selected="true" android:color="@color/White"/> //選擇時字體顏色
    <!-- pressed -->
    <item android:color="@color/Black"/> //未選擇時字體顏色
 
</selector>
 
2.設置要變色的testview屬性:
    <TextView
        android:id="@+id/txtItem0"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:height="70dp"
        android:text="Item0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@drawable/itemcolor_black"
        android:textSize="22dp" />