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


当前回答

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

其他回答

更新:

用这个代替 < 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:buttonTint属性在XML中这样做:

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radio"
    android:checked="false"
    android:padding="5dp"
    android:buttonTint="@color/radio_color"/>

你可以这样做,在Java中使用android:buttonTint:

// RadioButton ColorStateList
ColorStateList colorStateList = new ColorStateList(
    new int[][]{
        new int[]{-android.R.attr.state_checked}, // Unchecked
        new int[]{android.R.attr.state_checked} // Checked
    },

    new int[]{
        DataUtils.getColorResource(mContext, R.color.colorBlack), // Unchecked
        DataUtils.getColorResource(mContext, R.color.colorPrimary) // Checked
    }
);

RadioButton radio = findViewById(R.id.radio);   
radio.setButtonTintList(colorStateList);

这是21年之前和21年之后的API。

在你的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。

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

<!-- 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>
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radio"
    android:buttonTint="@color/my_color"/>

所有按钮都会改变颜色,包括圆形框和中央复选框。