无法在Android上更改单选按钮颜色

13

我正在使用Android Studio。我需要更改单选按钮的颜色,在将“Button Tint Color”值更改为所需的颜色后,它在预览中起作用,但每当我在设备上启动应用程序时,按钮都是标准的绿/蓝色。

这是某种设备API级别问题吗?如果是,那么是否可能为旧设备更改颜色?


请查看此帖子 https://dev59.com/PWQn5IYBdhLWcg3wETvj#52802572 - Rasoul Miri
8个回答

22

在阅读了@Ranjith的回答后,我进行了一些调查,发现这个方法似乎有效。只需将其添加到您的AppTheme即可。

    //red is the color of the pressed state and activated state
    <item name="colorControlActivated">@android:color/holo_red_light</item>
    //black is the color of the normal state
    <item name="colorControlNormal">@android:color/black</item>
我的问题是如何通过编程来做到这一点,因为我的单选按钮是动态的?

11

这可以通过两种方式实现(以支持Lollipop之前的版本):

  1. Use AppCompatRadioButton:

    AppCompatRadioButton radioButton;
    // now use following methods to set tint colour
    radioButton.setSupportButtonTintMode();
    radioButton.setSupportButtonTintList();
    
  2. Apply this as style to your RadioButton in your XML:

    <style name="RadioButton.Login" parent="Widget.AppCompat.CompoundButton.RadioButton">
        <item name="android:textColor">@android:color/white</item>
        <item name="buttonTint">@android:color/white</item>
    </style>
    

1
好的回答,感谢您包含向后兼容支持。 - AdamMc331

8
RadioButton raPrivate = (RadioButton) layout.findViewById(R.id.radioPrivate);
int textColor = Color.parseColor(#000000);
raPrivate.setButtonTintList(ColorStateList.valueOf(textColor));

10
看起来 buttonTint 属性只在 Android 5.0(API 21)及以上版本中可用。在此之前有什么替代方案吗? - AdamMc331
1
@McAdam331 请查看我在下面的回答 - Sufian
在多次尝试失败后,这个答案加上AppCompatRadioButton就是最终答案。谢谢。 - voghDev
太棒了!setSupportButtonTintList在API 21及以上版本上运行得非常好。 - sud007

5

如果您想更改颜色,但不想为整个应用程序更改colorControlActivated和colorControlNormal,则可以通过创建新样式来仅覆盖单选按钮的apptheme:

<android.support.v7.widget.AppCompatRadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true"
    android:theme="@style/RadioButtonTheme"
    android:id="@+id/radioButton"/>



<style name="RadioButtonTheme" parent="@style/AppTheme">
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlNormal">@color/colorPrimaryDark</item>
</style>

3
不需要额外的样式。Android支持通过xml实现。只需在单选按钮中添加android:buttonTint="@color/yourColor"即可。
例如:
<RadioButton
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:buttonTint="@color/red"
 android:text="Upload"
 android:textColor="@color/text_dark_gray"
 android:textSize="14sp" />

2
假设您的应用程序中使用了appcompat,请在styles.xml中添加以下内容:
<item name="colorAccent">@color/blue</item>

现在蓝色 colorAccent 将被设置,只需将颜色更改为您想要的任何颜色。

例如,整个 style.xml 文件:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/accent_material_dark</item>
        <item name="colorPrimaryDark">@color/accent_color_dark</item>
        <item name="colorAccent">@color/accent_color</item>
        <item name="windowActionBar">false</item>
    </style>
</resources>

@victor freg mark 回答了我的问题,如果我的回答对您有帮助,请标记一下,这样可以帮助其他用户。 - Psypher

1
我已经这样做了:

屏幕截图

enter image description here

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


    </RadioGroup>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        /**
         * First Radio Buttonn
         */
        RadioButton radioButtonAndroid = (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);
        radioButtonAndroid.setText("Hello Android");
        radioGroup.addView(radioButtonAndroid);

        /**
         * Second Radio Buttonn
         */
        RadioButton radioButtonIos = (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);
        radioButtonIos.setText("Hello Ios");
        radioGroup.addView(radioButtonIos);
    }
}

custom_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:buttonTint="@color/colorPrimary"
    android:text="">

</RadioButton>

希望这能对你有所帮助。

0
//red is the color of the pressed state and activated state
<item name="colorControlActivated">@android:color/holo_red_light</item>
//black is the color of the normal state
<item name="colorControlNormal">@android:color/black</item>

来自:user2968401

一旦您拥有了不同样式的单选按钮,您可以通过将它们分配给已设置为新样式的新单选按钮来交换它们:

(RadioButton)layout.findViewById(R.id.radioButton) = new RadioButton(this, null, R.style.RadioButton_style);

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