有一个listView,用selector设置条目背景。
list_selecter.xml :
<?xml version="1.0" encoding="utf-8"?>
<!-- focused and pressed -->
<item android:drawable="@color/darkred" android:state_pressed="true"/>
<item android:drawable="@android:color/transparent" android:state_pressed="false"/>
<!-- pressed -->
<item android:drawable="@color/darkred"/>
ListItem的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selecter"
android:orientation="vertical"
android:padding="10dp" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newsHeadingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:textColor="@color/text_selector" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@drawable/aerow" />
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newsText"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_below="@id/newsHeadingText"
android:textColor="#736F6E" />
</RelativeLayout>
但是颜色并没有发生改变,请指点迷津,谢谢。
ChatGPT尝试为您解答,仅供参考
看起来你的代码已经将 list_selecter.xml 作为背景设置给了 ListItem 布局文件中的 RelativeLayout 元素。
但是,由于你提供的 list_selecter.xml 中没有定义任何背景色,因此 ListItem 布局文件中的 RelativeLayout 元素的背景色并没有改变。
如果你想改变 ListItem 布局文件中的 RelativeLayout 元素的背景色,你可以在 list_selecter.xml 中定义背景色。例如:
<?xml version="1.0" encoding="utf-8"?>
<!-- focused and pressed -->
<item android:drawable="@color/darkred" android:state_pressed="true"/>
<item android:drawable="@color/gray" android:state_pressed="false"/>
<!-- pressed -->
<item android:drawable="@color/darkred"/>
这样,当你按下 ListItem 布局文件中的 RelativeLayout 元素时,背景色将会变成 @color/darkred,当你放开时,背景色将会变成 @color/gray。