我想在我的一个项目中改变RadioButton的圆圈的颜色,但我不知道该设置哪个属性。背景颜色是黑色的,所以它是看不见的。我想把圆圈的颜色设置为白色。
当前回答
只需在<RadioButton>标签上使用android:buttonTint="@color/colorPrimary"属性。
其他回答
我把它做成了这样的简短方式(在API 21之前和21之后工作):
XML中的单选按钮应该如下所示
<RadioButton android:id="@+id/radioid"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:button="@drawable/radiodraw" />
在radiodraw.xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" >
<shape android:shape="oval" >
<stroke android:width="1dp" android:color="#000"/>
<size android:width="30dp" android:height="30dp"/>
<solid android:color="@android:color/transparent"/>
</shape>
</item>
<item android:state_checked="true">
<layer-list>
<item>
<shape android:shape="oval">
<stroke android:width="1dp" android:color="#000"/>
<size android:width="30dp" android:height="30dp"/>
<solid android:color="@android:color/transparent"/>
</shape>
</item>
<item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
<shape android:shape="oval">
<solid android:width="1dp" android:color="#000"/>
<size android:width="10dp" android:height="10dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
你必须添加颜色透明绘制未检查状态;否则它会画一个纯黑色椭圆。
要以编程方式更改单选按钮的颜色,您可以使用以下方法:
yourradio button name.buttonDrawable?.setColorFilter(Color.parseColor( color_value), PorterDuff.Mode.SRC_ATOP)
你可以用android:buttonTint属性在XML中这样做:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:checked="false"
android:padding="5dp"
android:buttonTint="@color/radio_color"/>
你可以这样做,在Java中使用android:buttonTint:
// RadioButton ColorStateList
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked}, // Unchecked
new int[]{android.R.attr.state_checked} // Checked
},
new int[]{
DataUtils.getColorResource(mContext, R.color.colorBlack), // Unchecked
DataUtils.getColorResource(mContext, R.color.colorPrimary) // Checked
}
);
RadioButton radio = findViewById(R.id.radio);
radio.setButtonTintList(colorStateList);
最简单的方法是在值中更改colourAccent颜色-> colors .xml 但要注意,它也会改变其他东西,如编辑文本光标颜色等。
<颜色名称=“颜色强调”>#75aeff</颜色>
我有这个问题。如果你的应用程序有一个黑色背景,你有很多的radiobutton是不可见的背景,这是复杂的编辑android:buttonTint属性的每一个。最好的解决方案是更改styles.xml文件中的父主题
我改变了
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
to
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
因此,RadioButtons的圆圈变成了浅灰色,现在即使在黑色背景下也能看到。
这是我的style.xml文件:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
推荐文章
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?