一:常规的使用按下和正常生活图片的更改
1,在res/drawble下创建selector的xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@mipmap/icon_titilebar_message_right_pre" android:state_focused="true" android:state_pressed="false" />
<item android:drawable="@mipmap/icon_titilebar_message_right_pre" android:state_pressed="false" android:state_selected="true" />
<item android:drawable="@mipmap/icon_titilebar_message_right_pre" android:state_pressed="true" />
<item android:drawable="@mipmap/icon_titilebar_message_right_pre" android:state_checked="true" />
<item android:drawable="@mipmap/icon_titilebar_message_right_nor"></item>
</selector>
2,xml中每个空间设置background属性即可
<TextView
android:visibility=
"gone"
android:id=
"@+id/tv_drug_detail_jiyao"
android:layout_width=
"50dp"
android:layout_height=
"20dp"
android:layout_marginRight=
"10dp"
android:background=
"@drawable/selsctor_bg_drug_detailbe"
android:gravity=
"center"
android:text=
"基药"
android:textColor=
"@color/color_3e6398"
android:textSize=
"@dimen/text_font_13" />
到此完成
二:有些时候需求是不要图片而只要一个边框等等(其实性质是一样的,只是我们自己画)
1,看selector的xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="rectangle">
<corners android:bottomLeftRadius="@dimen/padding_5" android:bottomRightRadius="@dimen/padding_0" android:topLeftRadius="@dimen/padding_5" android:topRightRadius="@dimen/padding_0" />
<stroke android:width="0.5dp" android:color="@color/color_7791cd" />
<solid android:color="@color/color_7791cd" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:bottomLeftRadius="@dimen/padding_5" android:bottomRightRadius="@dimen/padding_0" android:topLeftRadius="@dimen/padding_5" android:topRightRadius="@dimen/padding_0" />
<stroke android:width="0.5dp" android:color="@color/color_7791cd" />
<solid android:color="@color/color_ffffff" />
</shape>
</item>
</selector>
2,布局中引用
<TextView
android:id=
"@+id/linear_drug_detail_info_down"
android:layout_width=
"0dp"
android:layout_height=
"30dp"
android:layout_weight=
"1"
android:background=
"@drawable/selector_tab_drug_detail_left"
android:gravity=
"center"
android:text=
"医保信息"
android:layout_marginRight=
"-0.5dp"
android:textColor=
"@drawable/selector_color_3e6398"
android:textSize=
"@dimen/text_font_15" />
三:有些时候文字颜色也需要跟着变化
1,文字颜色的selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/color_ffffff" android:state_selected="true">
</item>
<item android:color="@color/color_ffffff" android:state_pressed="true">
</item>
<item android:color="@color/color_ffffff" android:state_focused="true">
</item>
<item android:color="@color/color_3e6398">
</item>
</selector>
2,xml文件中引用
<TextView
android:id=
"@+id/linear_drug_detail_info_down"
android:layout_width=
"0dp"
android:layout_height=
"30dp"
android:layout_weight=
"1"
android:background=
"@drawable/selector_tab_drug_detail_left"
android:gravity=
"center"
android:text=
"医保信息"
android:layout_marginRight=
"-0.5dp"
android:textColor=
"@drawable/selector_color_3e6398"
android:textSize=
"@dimen/text_font_15" />
转载请注明原文地址: https://ju.6miu.com/read-1293387.html