转载请注明出处:http://blog.csdn.net/crazy1235/article/details/70036554
使用RadioButton时,如果需要自定义样式,则要注意使用 android:state_checked 这个属性。
举个例子:
一个选择性别的RadioGroup:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<RadioButton
style="@style/custom_rbtn"
android:layout_width="70dp"
android:text="男士" />
<RadioButton
style="@style/custom_rbtn"
android:layout_width="70dp"
android:layout_marginLeft="15dp"
android:text="女士" />
</RadioGroup>
style :
<style name="custom_rbtn">
<item name="android:layout_width">wrap_content</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:layout_height">30dp</item>
<item name="android:background">@drawable/selector_rbtn_bg</item>
<item name="android:button">@null</item>
<item name="android:gravity">center</item>
<item name="android:textColor">@color/selector_rbtn_text</item>
</style>
android:background & android:textColor 分别定义了selector
selector_rbtn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_bg_rbtn" android:state_checked="false" />
<item android:drawable="@drawable/shape_bg_rbtn_down" android:state_checked="true" />
</selector>
selector_rbtn_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/gray" android:state_checked="false" />
<item android:color="@android:color/white" android:state_checked="true" />
</selector>
如果该文件放在 res/drawable/ 路径下,则AS界面预览时会出现下面的error
但是不影响正常编译运行。
实际上主需要把这个文件放到 res/color/ 路径下即可。
再来说一下GridView
自定义GridView的item样式的时候,注意使用 android:state_activated 属性
item_tag_grid_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item_tag_tv"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:background="@drawable/selector_gv_item_bg"
android:gravity="center"
android:textColor="@color/selector_gv_tag_text" />
</LinearLayout>
selector_gv_item_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_bg_rbtn" android:state_activated="false" />
<item android:drawable="@drawable/shape_bg_rbtn_down" android:state_activated="true" />
</selector>
selector_gv_tag_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#a09ca4" android:state_activated="false" />
<item android:color="@android:color/white" android:state_activated="true" />
</selector>
各种selector的属性定义
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
参考:
https://developer.android.com/guide/topics/resources/color-list-resource.html
作者:crazy1235 发表于2017/4/14 16:20:32 原文链接
阅读:285 评论:0 查看评论