无法更改 Android 上的单选按钮颜色
Can't change Radio Button color on Android
我正在使用 Android Studio。我需要更改单选按钮的颜色,在将按钮色调颜色值更改为我需要的值后,它可以在预览中使用,但每当我在设备上启动应用程序时,按钮都是标准 green/blue-ish 颜色。
这是某种设备 API 级别的问题吗?如果是这样,是否可以更改旧设备的颜色?
假设您在应用中使用 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>
阅读@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>
我的问题是你如何以编程方式执行此操作,因为我有动态单选按钮?
RadioButton raPrivate = (RadioButton) layout.findViewById(R.id.radioPrivate);
int textColor = Color.parseColor(#000000);
raPrivate.setButtonTintList(ColorStateList.valueOf(textColor));
不需要额外的样式。 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" />
//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);
我这样做过:
截图
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>
希望对您有所帮助。
如果您想更改颜色,但不想更改整个应用的 colorControlActivated 和 colorControlNormal,您可以通过创建新样式来覆盖单选按钮的应用主题:
<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>
这可以通过两种方式完成(以支持 pre-Lollipop):
使用AppCompatRadioButton
:
AppCompatRadioButton radioButton;
// now use following methods to set tint colour
radioButton.setSupportButtonTintMode();
radioButton.setSupportButtonTintList();
将此作为样式应用到 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>
我正在使用 Android Studio。我需要更改单选按钮的颜色,在将按钮色调颜色值更改为我需要的值后,它可以在预览中使用,但每当我在设备上启动应用程序时,按钮都是标准 green/blue-ish 颜色。
这是某种设备 API 级别的问题吗?如果是这样,是否可以更改旧设备的颜色?
假设您在应用中使用 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>
阅读@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>
我的问题是你如何以编程方式执行此操作,因为我有动态单选按钮?
RadioButton raPrivate = (RadioButton) layout.findViewById(R.id.radioPrivate);
int textColor = Color.parseColor(#000000);
raPrivate.setButtonTintList(ColorStateList.valueOf(textColor));
不需要额外的样式。 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" />
//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);
我这样做过:
截图
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>
希望对您有所帮助。
如果您想更改颜色,但不想更改整个应用的 colorControlActivated 和 colorControlNormal,您可以通过创建新样式来覆盖单选按钮的应用主题:
<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>
这可以通过两种方式完成(以支持 pre-Lollipop):
使用
AppCompatRadioButton
:AppCompatRadioButton radioButton; // now use following methods to set tint colour radioButton.setSupportButtonTintMode(); radioButton.setSupportButtonTintList();
将此作为样式应用到 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>