您的位置:首页 > 其它

RadioButton自定义多选一效果

2016-02-18 13:42 85 查看

RadioButton自定义多选一效果

首先,准备一个selector文件,作为RadioButton的背景

这里要注意他们的状态:state-checked
1. <?xml version="1.0" encoding="utf-8"?>
2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
3.     <item
4.     android:state_checked="false"
5.     android:drawable="@drawable/tabswitcher_long" />
6.     <item
7.     android:state_checked="true"
8.     android:drawable="@drawable/tabswitcher_short" />
9. </selector>


在布局中引用该selector

重要属性:

android:checkedButton="@+id/rb_transition"
android:checkedButton="@+id/rb_transition":该属性的作用是,设置初始化状态下,指定默认选中的按钮的id。这里要特别的用+id,这是因为xml文件编译是有顺序的,要使用比自己后编译的id要使用“+id”;或者是在指定的RadioButton下设置android:checked="true"。

android:button="@null":该属性的作用可以去掉系统自带的背景图(就是一个空心圆和一个实心圆);使用后,再调用背景才能生效,否则会被默认图标遮盖
<RadioGroup
android:gravity="center"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/rb_0"
>
<RadioButton
android:id="@+id/rb_0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton1"
android:textSize="17.0sp"
android:textColor="@android:color/black"
android:gravity="center"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/radio"
/>
<RadioButton
android:id="@+id/rb_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton2"
android:textSize="17.0sp"
android:textColor="@android:color/black"
android:gravity="center"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/radio"
/>
<RadioButton
android:id="@+id/rb_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton3"
android:textSize="17.0sp"
android:textColor="@android:color/black"
android:gravity="center"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/radio"
/>
</RadioGroup>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RadioButton 自定义