自定义RadioButton样式1

来源:互联网 发布:最新淘宝白菜群2017 编辑:程序博客网 时间:2024/05/20 14:26


主要是布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><RadioGroup android:gravity="center"android:layout_gravity="bottom" android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="wrap_content" ><RadioButton     android:id="@+id/btn_0"android:textSize="17.0sp" android:textColor="@android:color/black"android:text="搜索微博"android:layout_weight="1" android:button="@null"android:checked="true"android:drawableLeft="@drawable/state_radio"android:background="@drawable/state_btn"android:gravity="center_vertical"></RadioButton><RadioButton     android:id="@+id/btn_1"android:textSize="17.0sp" android:textColor="@android:color/black"android:text="搜索用户"android:layout_weight="1" android:button="@null"android:drawableLeft="@drawable/state_radio"android:background="@drawable/state_btn"android:gravity="center_vertical"></RadioButton></RadioGroup></LinearLayout>

其中state_radio.xml
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_focused="false"android:state_checked="false"android:state_pressed="false"android:drawable="@drawable/bg_radio"></item><itemandroid:state_focused="false"android:state_checked="true"android:state_pressed="false"android:drawable="@drawable/bg_radio_selected"></item><itemandroid:state_focused="true"android:state_checked="false"android:state_pressed="false"android:drawable="@drawable/bg_radio_onfocus"></item><itemandroid:state_focused="true"android:state_checked="true"android:state_pressed="false"android:drawable="@drawable/bg_radio_onfocus_selected"></item></selector>

state_btn.xml:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">        <item android:state_window_focused="false"                   android:drawable="@drawable/bg_btn"/>         <item android:state_focused="true" android:state_pressed="true"                      android:drawable="@drawable/bg_btn_selected" />         <item android:state_focused="false" android:state_pressed="true"                 android:drawable="@drawable/bg_btn_selected" />  </selector>


上面的写法有点拖沓,采用style可能会更简单:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomTheme" parent="android:Theme">    <item name="android:radioButtonStyle">@style/RadioButton</item> </style> <style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">    <item name="android:button">@drawable/radio</item> </style> </resources> 

radio.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_checked="true" android:state_window_focused="false"         android:drawable="@drawable/radio_hover" />     <item android:state_checked="false" android:state_window_focused="false"         android:drawable="@drawable/radio_normal" />     <item android:state_checked="true" android:state_pressed="true"         android:drawable="@drawable/radio_active" />     <item android:state_checked="false" android:state_pressed="true"         android:drawable="@drawable/radio_active" />     <item android:state_checked="true" android:state_focused="true"         android:drawable="@drawable/radio_hover" />     <item android:state_checked="false" android:state_focused="true"         android:drawable="@drawable/radio_normal_off" />     <item android:state_checked="false" android:drawable="@drawable/radio_normal" />     <item android:state_checked="true" android:drawable="@drawable/radio_hover" /> </selector> 


实现分断Button,模仿MIUI设置页面顶部Button
http://blog.csdn.net/hudan2714/article/details/8228173
原创粉丝点击