效果图
按压前后效果对比:
主要代码
- colors.xml增加按压前后文字和背景颜色定义
<color name="text_bg">#FFFFFF</color>
<color name="text_pressed_bg">#FFC0CB</color>
<color name="text_color">#000000</color>
<color name="text_pressed_color">#0000FF</color>
- 增加背景选择器: selector_text_view_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/text_pressed_bg" android:state_pressed="true" />
<item android:drawable="@color/text_bg" android:state_pressed="false" />
</selector>
- 增加文字颜色选择器: selector_text_view_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/text_pressed_color" android:state_pressed="true" />
<item android:color="@color/text_color" android:state_pressed="false" />
</selector>
- 使用
注意: 需要设置android:clickable为true.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@drawable/selector_text_view_bg"
android:text="春风得意马蹄疾,一日看尽长安花。"
android:textColor="@drawable/selector_text_view_color" />
网友评论