【发布时间】:2011-09-08 21:12:56
【问题描述】:
我知道如何为我的单选按钮设置背景。但我不知道更改所选单选按钮背景的最佳/正确方法是什么?是否可以在xml中制作或必须在代码中完成?
问候
【问题讨论】:
标签: android background radio-button
我知道如何为我的单选按钮设置背景。但我不知道更改所选单选按钮背景的最佳/正确方法是什么?是否可以在xml中制作或必须在代码中完成?
问候
【问题讨论】:
标签: android background radio-button
只需在drawable文件夹中创建一个选择器xml文件
checkbox_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@color/checkbox_active" />
<item android:state_checked="false" android:drawable="@color/checkbox_inactive" />
</selector>
并将此背景应用于您的单选按钮
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/checkbox_background" />
【讨论】:
您可以按照此答案中的描述为您的单选按钮定义一个选择器:
Is it possible to change the radio button icon in an android radio button group
【讨论】:
使用
android:gravity="center"
单选按钮。这将使文本居中。
【讨论】: