公司项目的一个小需求,按住列表item的时候需要有背景颜色的变化,第一时间想到的就是selector实现。特此记录一下。
正常状态下
image.png
手指按住不放
image.png
selector代码:
// 整个条目北京selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white" android:state_window_focused="false" />
<item android:drawable="@color/lightgray" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="@color/lightgray" android:state_focused="false" android:state_pressed="true" />
<item android:drawable="@color/lightgray" android:state_selected="true" />
<item android:drawable="@color/lightgray" android:state_focused="true" />
</selector>
// 对号的selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white" android:state_window_focused="false" />
<item android:drawable="@drawable/item_right" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="@drawable/item_right" android:state_focused="false" android:state_pressed="true" />
<item android:drawable="@drawable/item_right" android:state_selected="true" />
<item android:drawable="@drawable/item_right" android:state_focused="true" />
</selector>
// 字体颜色selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="@color/blue" />
<item android:state_pressed="true" android:color="@color/blue" />
<item android:state_selected="true" android:color="@color/blue" />
<item android:color="@color/text_black" />
</selector>
网友评论