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


当前回答

创建一个可绘制文件my_compound_button_color_selector.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/text_app_color"/>
<item android:state_checked="true" android:color="@color/text_app_color"/>
<item android:color="@color/gray"/> </selector>

在style.xml文件中添加样式

<style name="AppRadioAppStyle" parent="Widget.AppCompat.CompoundButton.RadioButton">
    <item name="android:textColor">@drawable/my_compound_button_color_selector</item>
    <item name="drawableTint">@drawable/my_compound_button_color_selector</item>
</style>

布局文件

<RadioButton
                    android:id="@+id/radio1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:button="@null"
                    android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
                    android:gravity="center_horizontal|bottom"
                    android:background="@drawable/app_border_0"
                    android:padding="@dimen/_15sdp"
                    android:text="@string/no"
                    android:fontFamily="@font/poppins_medium"
                    style="@style/AppRadioAppStyle"
                    android:layout_marginStart="@dimen/_10sdp"/>

应该添加android:button="@null"在你的单选按钮

其他回答

这是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。

在res/values/colors.xml文件中,RadioButton默认采用colorAccent的颜色。 到那个文件中修改值

< [name = " colorAccent > # 3F51B5 < / [>

到你想要的颜色。

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radio"
    android:buttonTint="@color/my_color"/>

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

只需在<RadioButton>标签上使用android:buttonTint="@color/colorPrimary"属性。

@jh314正确。

在文件AndroidManifest.xml中,

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"></application>

在文件style.xml中:

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">@color/red</item>
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

项目名称必须为colorAccent。它决定了应用程序小部件的默认颜色。

但如果你想在代码中改变颜色,也许@aknay的答案是正确的。