我正在使用Android Studio。我需要更改单选按钮的颜色,在将“Button Tint Color”值更改为所需的颜色后,它在预览中起作用,但每当我在设备上启动应用程序时,按钮都是标准的绿/蓝色。
这是某种设备API级别问题吗?如果是,那么是否可能为旧设备更改颜色?
在阅读了@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>
我的问题是如何通过编程来做到这一点,因为我的单选按钮是动态的?这可以通过两种方式实现(以支持Lollipop之前的版本):
Use AppCompatRadioButton
:
AppCompatRadioButton radioButton;
// now use following methods to set tint colour
radioButton.setSupportButtonTintMode();
radioButton.setSupportButtonTintList();
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>
RadioButton raPrivate = (RadioButton) layout.findViewById(R.id.radioPrivate);
int textColor = Color.parseColor(#000000);
raPrivate.setButtonTintList(ColorStateList.valueOf(textColor));
AppCompatRadioButton
就是最终答案。谢谢。 - voghDevsetSupportButtonTintList
在API 21及以上版本上运行得非常好。 - sud007如果您想更改颜色,但不想为整个应用程序更改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>
<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" />
<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>
屏幕截图
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>
//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);