我想在我的一个项目中改变RadioButton的圆圈的颜色,但我不知道该设置哪个属性。背景颜色是黑色的,所以它是看不见的。我想把圆圈的颜色设置为白色。
当前回答
适用于api21
创建一个自定义样式的RadioButton:
文件style.xml
<style name="RadioButton" parent="Theme.AppCompat.Light">
<item name="colorAccent">@color/green</item>
<item name="android:textColorSecondary">@color/mediumGray</item>
<item name="colorControlNormal">@color/red</item>
</style>
在布局中,使用主题:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RadioButton" />
适用于API 21及以上
只需使用buttonTint:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/green" />
其他回答
使用app:buttonTint而不是android:buttonTint,像这样:
<com.google.android.material.radiobutton.MaterialRadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="English"
android:checked="true"
app:buttonTint="#FF0000"
android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
android:layout_marginHorizontal="16dp"
android:layoutDirection="rtl"
/>
or
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="English"
android:checked="true"
app:buttonTint="#FF0000"
android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
android:layout_marginHorizontal="16dp"
android:layoutDirection="rtl"
/>
设置buttonTint属性。例如,android:buttonTint="#99FF33"。
设置buttonTint颜色更简单(只适用于API级别21或以上):
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:checked="true"
android:buttonTint="@color/your_color"/>
在values/colors.xml文件中,输入颜色,在本例中是红色:
<color name="your_color">#e75748</color>
结果:
如果你想通过代码(也是API 21及以上):
if(Build.VERSION.SDK_INT >= 21)
{
ColorStateList colorStateList = new ColorStateList(
new int[][]
{
new int[]{-android.R.attr.state_enabled}, // Disabled
new int[]{android.R.attr.state_enabled} // Enabled
},
new int[]
{
Color.BLACK, // disabled
Color.BLUE // enabled
}
);
radio.setButtonTintList(colorStateList); // set the color tint list
radio.invalidate(); // Could not be necessary
}
它有一个XML属性:
android:buttonTint="yourcolor"
如果你有android:buttonTint,它不会工作,你必须改变它到app:buttonTint。 在升级到androidx之后,我不得不这样做。
推荐文章
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?