您的位置:首页 > 移动开发 > Android开发

Android开发笔记: 自定义RadioButton样式

2017-10-21 10:14 417 查看

1 先看效果图



2 准备图片

  注意图片不要太大,我的是30x30的



3 导入图片资源

   将cb_normal.png和cb_selected.png放入drawable里

4 创建radio_button_state.xml

  在drawable里创建该文件,如下:

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/cb_normal" />
<item
android:state_checked="true"
android:drawable="@drawable/cb_selected" />
</selector>

5 添加布局文件

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:id="@+id/rb_edid_switch_edid"
android:text="切换EDID"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:textColor="@color/app_text"
android:checked="true"
android:button="@null"
android:background="@drawable/fun_button_highlight"
android:drawableLeft="@drawable/radio_button_state"
android:layout_height="60dp" />

<RadioButton
android:id="@+id/rb_edid_save_edid"
android:text="保存EDID"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:textColor="@color/app_text"
android:button="@null"
android:background="@drawable/fun_button_highlight"
android:drawableLeft="@drawable/radio_button_state"
android:layout_height="60dp" />

</RadioGroup>


6 总结

  需要将RadioButton的button属性设置为null,否则会有之前的选择框,

即android:button="@null"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息