更改单选按钮的圆圈颜色

218

我想要改变我的一个项目RadioButton的圆圈颜色,但是我不知道应该设置哪个属性。背景颜色是黑色,所以它看起来是透明的。我想将圆圈的颜色设置为白色。


请参考此链接:http://heliodorj.blogspot.in/2009/04/androids-statelistdrawable-example.html - Android Developer
使用两个图像来表示单选按钮的选中状态和未选中状态,通过使用setbackground资源或使用selector xml,在单选按钮点击时更改这些图像。 - SAURABH_12
27个回答

386

如果仅适用于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>

结果:

彩色 Android 单选按钮

如果您想通过代码实现(也适用于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
}

2
@emaillenin,如果您想通过代码更改颜色色调,可以使用control.getDrawable().setColorFilter(getResources().getColor(color), PorterDuff.Mode.SRC_IN);,其中control是您想更改色调的控件,而color则是您想要的颜色的整数值,例如R.color.red - Jorge Arimany
1
@JorgeArimany 对于一个 RadioButton,是使用 getButtonDrawable 还是 getCompoundDrawables 方法?getCompoundDrawables 方法返回的是一个列表。 - Raj
@emaillenin,谢谢你。我的回答是针对其他控件(如按钮)的,我已经更新了我的答案并添加了你要找的代码,希望能帮到你。 - Jorge Arimany
4
@JorgeArimany 我已经让它在大于21的情况下正常工作了,现在我正在寻找小于21的具体答案。 - Raj
3
要更改选中按钮的颜色,您可以添加或替换一个带有android.R.attr.state_checked状态并添加颜色。 - 6rchid
如果您想为单选按钮添加描边和背景,请将 android:buttonTintandroid:background 结合使用。 - mochadwi

178

更新:

  1. 使用这一个代替之前的

<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>
    
    1. 在您的程序中,您应该像这样调用它: 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上测试的结果:

    这张图片是在API 19上测试的

    查看Android 参考链接以获取更多详细信息。


    10
    这个答案应该被采纳。如果你想通过xml添加这个支持的单选按钮,请使用<android.support.v7.widget.AppCompatRadioButton ../> - Vinayak Garg
    22
    setSupportButtonTintList是一个你不应该使用的私有方法。在某些版本的Android上,单选按钮的行为会变得奇怪。相反,使用CompoundButtonCompat.setButtonTintList(rb, colorStateList) - wampastompa
    2
    @wampastompa说得对。在API 22上,结果是当选中时我只看到一个外圆,从未填充。@aknay,请更新您的答案。 - Nino van Hooff
    1
    我目前在使用API 16。我按照上面列出的步骤添加了AppCompat单选按钮,但它仍然没有正确显示。 - tccpg288
    1
    你不需要任何代码,可以参考IgorOliveira的答案。适用于所有版本。 - Kirill Karmazin

    82
    <android.support.v7.widget.AppCompatRadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:buttonTint="@color/Color" />
    

    11
    这是适用于棒棒糖之前、当前和之后设备的答案!太棒了。 - sdelvalle57
    1
    这应该是被接受的答案。简短而完美。注意:使用 app:buttonTint="@color/Color" 而不是 andoid: buttonTint="@color/Color"! - Kirill Karmazin
    @KirillKarmazin 接受的应该是小于21的内容。 - user924
    不过,需要解释一下。请通过编辑(更改)您的答案来回复,而不是在评论中回复(不要包含“Edit:”,“Update:”或类似内容 - 答案应该看起来像是今天写的)。 - Peter Mortensen

    62

    这个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


    我该如何以编程的方式实现这个? - tccpg288
    如果您使用此xml并且想在编程中更改颜色,只需使用radioButton.setChecked(false)或radioButton.setChecked(true)。 - Vaibhav Sharma
    我不明白你的问题。能否请你详细说明一下? - Vaibhav Sharma
    我的错误,如果我使用常规的RadioButton或AppCompat RadioButton有关系吗? - tccpg288
    1
    它不起作用,如果我初始化RadioButton时怎么办?这是我的代码:mPollAnswerArrayList.add((indexCreated), new RadioButton((getActivity().getApplicationContext()),null,R.style.radiobutton)); - tccpg288
    显示剩余3条评论

    40
    1. 在你的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


    4
    这应该被接受为答案,它适用于所有版本并且是最简洁的。 - Antonis Radz
    我不得不使用<item name="android:colorControlActivated">@color/pink</item>才能让它对我起作用。我仍然不确定为什么。否则,这是一个很好的答案。 - user8425970
    当你开始考虑浅色和深色主题时,这是一个更好的解决方案。 - David Kariuki

    23

    适用于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" />
    

    应该澄清:buttonTint 对于 API 21 使用 AppCompat/AndroidX 的应用程序都有效,不受 API 版本的限制。 - Chisko

    20

    您可以通过在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>
    

    您可以在该样式中设置所需的颜色。


    RadioButton内的主题不起作用(再也没有了?)。单击按钮时会崩溃,因为找不到onClick方法,尽管它在这里。 - Bevor
    这个问题可能有其他原因。在实现onClick之前,请确保获取正确的视图ID。 - Jaura

    19

    你必须使用这段代码:

    <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


    这是最佳解决方案!简洁、短小并且适用于旧版本。 - John T

    12

    设置buttonTint属性。例如,android:buttonTint="#99FF33"


    9
    仅适用于 API 21 及以上版本。 - miracle-doh

    11

    有时候你只需要像这样覆盖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 用于已选中的状态。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接