我通过编程方式在 LinearLayout
中创建了 RadioButton
和 CheckBox
,但现在我想更改单选按钮和复选框的颜色。我使用以下代码:
RadioButton.setHighlightColor(Color.parseColor("#0c83bd"));
checkbox.setHighlightColor(Color.parseColor("#0c83bd"));
但是它并没有生效。
我通过编程方式在 LinearLayout
中创建了 RadioButton
和 CheckBox
,但现在我想更改单选按钮和复选框的颜色。我使用以下代码:
RadioButton.setHighlightColor(Color.parseColor("#0c83bd"));
checkbox.setHighlightColor(Color.parseColor("#0c83bd"));
但是它并没有生效。
请使用 AppCompatCheckBox 和 AppCompatRadioButton 代替 CheckBox 和 RadioButton。 您的xml将包含:
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/cbSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorAccent" //This to set your default button color
android:checked="true"/>
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rb"
app:buttonTint="@color/colorAccent" //This to set your default button color
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
现在是Java代码部分: 创建 ColorStateList
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_enabled} //enabled
},
new int[] {getResources().getColor(R.color.colorPrimary) }
);
要以编程方式更改 AppCompatRadioButton 或 AppCompatCheckBox 的颜色,请调用 setSupportButtonTintList 。
AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
radioButton.setSupportButtonTintList(colorStateList);
AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
cbSelected.setSupportButtonTintList(colorStateList);
您可以这样创建动态的RadioButton
:
RadioButton male = new RadioButton(ReservationContact.this);
male.setTextColor(Color.BLACK);
male.setText("Male");
male.setSelected(true);
male.setId(0);
这用于更改RadioButton
圆圈的默认颜色。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background)));
}
male.setHighlightColor(getResources().getColor(R.color.background));
试试这个
AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);
替代
RadioGroup newRadioButton = new RadioGroup(this);
CheckBox newCheckBox = new CheckBox(this);
对于CheckBox
,请将您的CheckBox
替换为AppCompatCheckBox
并调用以下方法:
public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) {
ColorStateList colorStateList = new ColorStateList(
new int[][] {
new int[] { -android.R.attr.state_checked }, // unchecked
new int[] { android.R.attr.state_checked } // checked
},
new int[] {
uncheckedColor,
checkedColor
}
);
checkBox.setSupportButtonTintList(colorStateList);
}
我认为对于RadioButton
也应该是类似的。您可以在android.R.attr
中检查声明的属性并更改代码。
我尝试了几种编程方法来实现它,但除了以下方法外,都没有成功。在这里放下来,以防有人觉得有用。
<androidx.appcompat.widget.AppCompatRadioButton
android:id="@+id/radio_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null" />
private fun getRadioButtonColor(): ColorStateList {
val states = arrayOf(
intArrayOf(-android.R.attr.state_checked),
intArrayOf(android.R.attr.state_checked))
val colors = intArrayOf(
ContextCompat.getColor(context, R.color.grey2),
ContextCompat.getColor(context, R.color.actionBlue)
)
return ColorStateList(states, colors)
}
然后使用CompoutButtonCompat类中的setButtonTintList方法来设置这个colorStateList。
CompoundButtonCompat.setButtonTintList(radio_btn, getRadioButtonColor())
app:buttonTint="@color/yellow"
你的 xml 示例:
<RadioButton
android:id="@+id/radio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:buttonTint="@color/yellow"
android:text=" Radio"
android:textColor="@color/yellow" />
我在继续回答ywwynm的问题。
Google将setSupportButtonTintList设置为受限,因此无法使用它。
解决方法是将按钮作为TintableCompoundButton接口使用,该方法未受限制。
适用于API 19+的AppCompatRadioButton。 AppCompatCheckbox实现了相同的接口,理论上也应该可以工作,但我还没有测试过。
祝玩得愉快 :)
public static void setAppCompatRadioButtonColor(AppCompatRadioButton radioButton, int uncheckedColor, int checkedColor) {
ColorStateList colorStateList = new ColorStateList(
new int[][] {
new int[] { -android.R.attr.state_checked }, // unchecked
new int[] { android.R.attr.state_checked } // checked
},
new int[] {
uncheckedColor,
checkedColor
}
);
((TintableCompoundButton) radioButton).setSupportButtonTintList(colorStateList);
}
RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);
rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
rb.setButtonDrawable(R.drawable.image);
rb.setHighlightColor(Color.parseColor("#0c83bd"));
}
});
}