Androidstudio用radiobutton设置背景
时间: 2024-06-14 08:03:31 浏览: 16
以下是在Android Studio中使用RadioButton设置背景的方法:
1. 在drawable目录下新建一个selector文件,例如radio_button_bg.xml。
2. 在selector文件中添加两个item,分别对应选中和未选中状态,并设置对应的背景样式。
3. 在RadioButton的XML布局文件中设置background属性为刚才创建的selector文件即可。
示例代码如下:
1. 在drawable目录下新建一个selector文件radio_button_bg.xml:
```xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="#AA6600"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:state_checked="false">
<shape>
<stroke android:width="1dp" android:color="#AA6600"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
```
2. 在RadioButton的XML布局文件中设置background属性为刚才创建的selector文件:
```xml
<RadioButton
android:id="@+id/radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:background="@drawable/radio_button_bg"/>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)