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

时间:2015-02-03 19:51:06

标签: android button colors

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

这是某种设备API级问题吗?如果是这样,是否可以更改旧设备的颜色?

8 个答案:

答案 0 :(得分:18)

在阅读了@ 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>

我的问题是你是如何编程的,因为我有动态单选按钮?

答案 1 :(得分:9)

这可以通过两种方式完成(支持pre-Lollipop):

  1. 使用AppCompatRadioButton

    AppCompatRadioButton radioButton;
    // now use following methods to set tint colour
    radioButton.setSupportButtonTintMode();
    radioButton.setSupportButtonTintList();
    
  2. 将其作为样式应用于XML中的RadioButton

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

答案 2 :(得分:5)

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

答案 3 :(得分: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" />

答案 4 :(得分:3)

如果您想更改颜色,但不更改整个应用程序的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>

答案 5 :(得分: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>

答案 6 :(得分:1)

我这样做了:

<强>截图

enter image description here

<强> activity_main.xml中

has_many :items,:dependent=>:destroy

<强> MainActivity.java

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

<强> custom_radiobutton.xml

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

希望这会对你有所帮助。

答案 7 :(得分: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);
相关问题