我想要改变我的一个项目中RadioButton的圆圈颜色,但是我不知道应该设置哪个属性。背景颜色是黑色,所以它看起来是透明的。我想将圆圈的颜色设置为白色。
我想要改变我的一个项目中RadioButton的圆圈颜色,但是我不知道应该设置哪个属性。背景颜色是黑色,所以它看起来是透明的。我想将圆圈的颜色设置为白色。
如果仅适用于API级别21或更高级别,则更简单的方法是设置按钮的tint颜色:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:checked="true"
android:buttonTint="@color/your_color"/>
在你的values/colors.xml文件中,放入你的颜色,这种情况下是红棕色:<color name="your_color">#e75748</color>
结果:
如果您想通过代码实现(也适用于API 21及以上):
if(Build.VERSION.SDK_INT >= 21)
{
ColorStateList colorStateList = new ColorStateList(
new int[][]
{
new int[]{-android.R.attr.state_enabled}, // Disabled
new int[]{android.R.attr.state_enabled} // Enabled
},
new int[]
{
Color.BLACK, // disabled
Color.BLUE // enabled
}
);
radio.setButtonTintList(colorStateList); // set the color tint list
radio.invalidate(); // Could not be necessary
}
control.getDrawable().setColorFilter(getResources().getColor(color), PorterDuff.Mode.SRC_IN);
,其中control
是您想更改色调的控件,而color
则是您想要的颜色的整数值,例如R.color.red
。 - Jorge Arimanyandroid.R.attr.state_checked
状态并添加颜色。 - 6rchidandroid:buttonTint
与 android:background
结合使用。 - mochadwi更新:
使用这一个代替之前的
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rbtn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/primary" />
然后将此行添加到父布局中,或在Android Studio中按Alt + Enter自动添加:xmlns:app="http://schemas.android.com/apk/res-auto"
一个最简示例应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rbtn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/primary" />
</LinearLayout>
AppCompatRadioButton radioButton = (AppCompatRadioButton) view.findViewById(R.id.rbtn_test);
基本上,这种模式可以应用于所有 AppCompact 类型,例如 AppCompatCheckBox、AppCompatButton 等等。
旧答案:
为了支持 Android API 21 以下的版本,您可以使用AppCompatRadioButton。然后使用 setSupportButtonTintList
方法来更改颜色。这是我的代码片段,用于创建单选按钮。
AppCompatRadioButton rb;
rb = new AppCompatRadioButton(mContext);
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.DKGRAY
, Color.rgb (242,81,112),
}
);
rb.setSupportButtonTintList(colorStateList);
在API 19上测试的结果:
查看Android 参考链接以获取更多详细信息。
<android.support.v7.widget.AppCompatRadioButton ../>
。 - Vinayak GargsetSupportButtonTintList
是一个你不应该使用的私有方法。在某些版本的Android上,单选按钮的行为会变得奇怪。相反,使用CompoundButtonCompat.setButtonTintList(rb, colorStateList)
。 - wampastompa<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/Color" />
这个API可以在21之前和21之后都使用。
在你的styles.xml
中添加:
<!-- custom style -->
<style name="radionbutton"
parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:button">@drawable/radiobutton_drawable</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
您的XML中的单选按钮应该长这样:
<RadioButton
android:layout_width="wrap_content"
style="@style/radionbutton"
android:checked="false"
android:layout_height="wrap_content"
/>
现在,您只需要在drawable文件夹
中创建一个radiobutton_drawable.xml
。这是您需要放入其中的内容:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_focused="false"/>
<item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_focused="false"/>
</selector>
您的radio_unchecked.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="1dp" android:color="@color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
您的radio_checked.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="1dp" android:color="@color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
</item>
<item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
<shape android:shape="oval">
<solid android:width="1dp" android:color="@color/colorAccent"/>
<size android:width="10dp" android:height="10dp"/>
</shape>
</item>
</layer-list>
只需用您选择的颜色替换 @color/colorAccent
。
在你的styles.xml文件中声明自定义样式。
<style name="MyRadioButton" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/indigo</item>
<item name="colorControlActivated">@color/pink</item>
</style>
将该样式通过 android:theme 属性应用于您的 RadioButton。 <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Radio Button"
android:theme="@style/MyRadioButton"/>
但前提是您的活动扩展了AppCompatActivity
。
<item name="android:colorControlActivated">@color/pink</item>
才能让它对我起作用。我仍然不确定为什么。否则,这是一个很好的答案。 - user8425970适用于API 21以下版本:
创建自定义样式的单选按钮:
文件style.xml
<style name="RadioButton" parent="Theme.AppCompat.Light">
<item name="colorAccent">@color/green</item>
<item name="android:textColorSecondary">@color/mediumGray</item>
<item name="colorControlNormal">@color/red</item>
</style>
在布局中,使用主题:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RadioButton" />
适用于API 21及更高版本
只需使用buttonTint:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/green" />
您可以通过在XML中使用样式来更改单选按钮未选中和选中状态的颜色。
<RadioButton
android:id="@+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RadioButtonStyle" />
在 style.xml 中。<style name="RadioButtonStyle" parent="Theme.AppCompat.Light">
<item name="colorAccent">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
</style>
您可以在该样式中设置所需的颜色。
你必须使用这段代码:
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radiobutton1"
app:buttonTint="@color/black" />
使用app:buttonTint
替代 android:buttonTint
,同时使用 android.support.v7.widget.AppCompatRadioButton
替代 Radiobutton
!
设置buttonTint
属性。例如,android:buttonTint="#99FF33"
。
有时候你只需要像这样覆盖colorControlNormal:
<style name="RadioButtonStyle" parent="AppTheme">
<item name="colorControlNormal">@color/pink</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:textColorSecondary">@color/black</item>
</style>
然后您将得到一个按钮,如下所示:
colorControlNormal 用于未选中的状态,colorAccent 用于已选中的状态。