我想在我的一个项目中改变RadioButton的圆圈的颜色,但我不知道该设置哪个属性。背景颜色是黑色的,所以它是看不见的。我想把圆圈的颜色设置为白色。


当前回答

对于那些想要更改禁用、检查和启用状态的用户,您可以执行以下步骤:

<!-- Or androidX radio button or material design radio button -->
<android.support.v7.widget.AppCompatRadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:buttonTint="@color/black"
                    android:text="Radiobutton1"
                    app:buttonTint="@color/radio_button_color" />

然后在color res文件夹中,创建一个名为“radio_button_color.xml”的文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/yellow900" android:state_selected="true" />
    <item android:color="@color/yellow800" android:state_checked="true" />
    <item android:color="@color/gray800" android:state_enabled="false" />
    <item android:color="@color/yellow800" android:state_enabled="true" />
</selector>

其他回答

设置buttonTint颜色更简单(只适用于API级别21或以上):

<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
}

我把它做成了这样的简短方式(在API 21之前和21之后工作):

XML中的单选按钮应该如下所示

  <RadioButton android:id="@+id/radioid"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:button="@drawable/radiodraw" />

在radiodraw.xml文件中:

  <?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_checked="false" >
          <shape  android:shape="oval" >
              <stroke android:width="1dp" android:color="#000"/>
              <size android:width="30dp" android:height="30dp"/>
              <solid android:color="@android:color/transparent"/>
          </shape>
      </item>
      <item android:state_checked="true">
          <layer-list>
              <item>
                  <shape android:shape="oval">
                      <stroke android:width="1dp" android:color="#000"/>
                      <size android:width="30dp" android:height="30dp"/>
                      <solid android:color="@android:color/transparent"/>
                  </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="#000"/>
                      <size android:width="10dp" android:height="10dp"/>
                  </shape>
              </item>
          </layer-list>
      </item>
  </selector>

你必须添加颜色透明绘制未检查状态;否则它会画一个纯黑色椭圆。

使用app:buttonTint而不是android:buttonTint,像这样:

 <com.google.android.material.radiobutton.MaterialRadioButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="English"
        android:checked="true"
        app:buttonTint="#FF0000"
        android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
        android:layout_marginHorizontal="16dp"
        android:layoutDirection="rtl"
        />

or

<RadioButton
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="English"
    android:checked="true"
    app:buttonTint="#FF0000"
    android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
    android:layout_marginHorizontal="16dp"
    android:layoutDirection="rtl"
    />

更新:

用这个代替 < android.support.v7.widget.AppCompatRadioButton android: id =“@ + id / rbtn_test” android: layout_width = " wrap_content " android: layout_height = " wrap_content " 应用:buttonTint = " @color /主" / > 然后将这一行添加到父布局或在Android Studio中按Alt + Enter自动添加 xmlns:应用= " 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
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:buttonTint="@color/Color" />