Android 轮播里面的 RadioButton 的 自定义

本文介绍了如何在Android轮播图中自定义RadioButton的样式,包括改变背景颜色和图片,通过设置selector和shape资源文件。同时,取消原有按钮样式,设置`android:button="@null"`,并在XML布局中添加RadioButton时应用自定义样式。

0: 首先可以自定义背景的颜色,图片,通过设置background="@drawable/background"

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:button="@null"
    android:background="@drawable/background"/>

1:然后需要在drawable 里面右键新建一个background的xml 文件,rootElement 为selector,



注意这里是判断是否为checked,然后再这里的drawable 后面填写相应的图片例如是@mipmap/start,( 我这里已经修改为了相应的shape的了。)


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

2:其次可以取消原有的按钮样式,通过android:button="@null"

3:定义不同的尺寸作为背景,而不是背景图片了,

右键drawable 创建文件选择shape 作为rootElement.例如

在shape 里面有一个属性为android:shape 来设置它的样子可以设置为oval 然后设置宽高相等就是一个圆形了。提示:在shape 里面可以通过设置stroke 来似的每一个radio 之间有间距的感觉,把颜色设置为透明色#0fff就可以了。-------------------------------------------shape_radio_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <size android:width="30dp" android:height="30dp"></size>
    <solid android:color="@android:color/holo_blue_dark"/>
    <stroke android:width="20dp" android:color="#0fff"></stroke>
</shape>

-------------------------------------------
shape_radio.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size android:height="30dp" android:width="30dp"></size>
    <solid android:color="@android:color/white"></solid>
    <stroke android:width="20dp" android:color="#0fff"></stroke>
</shape>



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
然后添加到布局中有两种方法:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
在代码中添加RadioButton 
mRadioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);
for(int i=0;i<mListUrl.size();i++){
    RadioButton  radioButton = new RadioButton(this);
    radioButton.setButtonDrawable(R.drawable.background);
    radioButton.setId(i);//为了联动的需要
    mRadioGroup.addView(radioButton);
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

在xml布局中添加RadioButton 一定要设置button="@null"

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@null"
    android:background="@drawable/background"/>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

可以将RadioButton的属性提取为样式 :

首先对xml里面的radiobutton的样式进行提取,右键,Refactor->extract->style



最后得到<RadioButtonstyle="@style/radio_style"/>


然后再values 下面的styles.xml文件里面找到。
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="radio_style">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:background">@drawable/background</item>
    </style>

</resources>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值