Can't change Radio Button color on Android

2019-02-08 14:50发布

I'm using Android Studio. I need to change the color of the Radio Button, after changing the Button Tint Color value to the one I need it works on the preview, but whenever I launch the app on a device the button is the standard green/blue-ish color.

Is this some kind of device API level issue? If so, is it possible to change the color for older devices?

8条回答
啃猪蹄的小仙女
2楼-- · 2019-02-08 15:29

No need of additional styling. Android supports it via xml. Just add android:buttonTint="@color/yourColor" in your radio button.

For eg.

<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" />
查看更多
劳资没心,怎么记你
3楼-- · 2019-02-08 15:41

This can be done in two ways (to support pre-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>
    
查看更多
一纸荒年 Trace。
4楼-- · 2019-02-08 15:43
//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>

From: user2968401

Once you have different styles for the radio button you can swap them by assigning them to a new Radio Button with the style already set to the new style:

(RadioButton)layout.findViewById(R.id.radioButton) = new RadioButton(this, null, R.style.RadioButton_style);
查看更多
Emotional °昔
5楼-- · 2019-02-08 15:46

Assuming you are using appcompat in your app just add the below within styles.xml

<item name="colorAccent">@color/blue</item>

Now blue colorAccent will be set, just change color to any color you want.

For eg, the whole 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>
查看更多
劳资没心,怎么记你
6楼-- · 2019-02-08 15:47

After reading @Ranjith's answer i did a little digging and this seemed to work. Just add it to your 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>

My question is how do you do this programatically as I have dynamic radio buttons??

查看更多
Ridiculous、
7楼-- · 2019-02-08 15:49
RadioButton raPrivate = (RadioButton) layout.findViewById(R.id.radioPrivate);
int textColor = Color.parseColor(#000000);
raPrivate.setButtonTintList(ColorStateList.valueOf(textColor));
查看更多
登录 后发表回答