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

被折叠的 条评论
为什么被折叠?



