我想在我的一个项目中改变RadioButton的圆圈的颜色,但我不知道该设置哪个属性。背景颜色是黑色的,所以它是看不见的。我想把圆圈的颜色设置为白色。
设置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
}
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:buttonTint="@color/my_color"/>
所有按钮都会改变颜色,包括圆形框和中央复选框。
更新:
用这个代替 < android.support.v7.widget.AppCompatRadioButton android: id =“@ + id / rbtn_test” android: layout_width = " wrap_content " android: layout_height = " wrap_content " 应用:buttonTint = " @color /主" / > 然后将这一行添加到父布局或在Android Studio中按Alt + Enter自动添加 xmlns:应用= " http://schemas.android.com/apk/res-auto "
最小示例应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rbtn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/primary" />
</LinearLayout>
在你的程序中,你应该这样调用它: AppCompatRadioButton radioButton = (AppCompatRadioButton) view.findViewById(R.id.rbtn_test);
基本上,这种模式可以应用于所有AppCompact类型,如AppCompatCheckBox、AppCompatButton等。
旧的回答:
为了支持以下android API 21,你可以使用AppCompatRadioButton。然后使用setSupportButtonTintList方法来改变颜色。这是我创建单选按钮的代码片段。
AppCompatRadioButton rb;
rb = new AppCompatRadioButton(mContext);
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.DKGRAY
, Color.rgb (242,81,112),
}
);
rb.setSupportButtonTintList(colorStateList);
API 19的测试结果:
详见Android参考链接。
这是21年之前和21年之后的API。
在你的styles.xml中输入:
<!-- custom style -->
<style name="radionbutton"
parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:button">@drawable/radiobutton_drawable</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
XML中的单选按钮应该如下所示:
<RadioButton
android:layout_width="wrap_content"
style="@style/radionbutton"
android:checked="false"
android:layout_height="wrap_content"
/>
现在你所需要做的就是在你的drawable文件夹中创建一个radiobutton_drawable.xml。以下是你需要加入的内容:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_focused="false"/>
<item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_focused="false"/>
</selector>
你的radio_unchecked.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="1dp" android:color="@color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
你的radio_checked.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="1dp" android:color="@color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</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="@color/colorAccent"/>
<size android:width="10dp" android:height="10dp"/>
</shape>
</item>
</layer-list>
用你选择的颜色替换@color/colorAccent。
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/Color" />
@jh314正确。
在文件AndroidManifest.xml中,
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"></application>
在文件style.xml中:
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">@color/red</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
项目名称必须为colorAccent。它决定了应用程序小部件的默认颜色。
但如果你想在代码中改变颜色,也许@aknay的答案是正确的。
我把它做成了这样的简短方式(在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>
你必须添加颜色透明绘制未检查状态;否则它会画一个纯黑色椭圆。
在res/values/colors.xml文件中,RadioButton默认采用colorAccent的颜色。 到那个文件中修改值
< [name = " colorAccent > # 3F51B5 < / [>
到你想要的颜色。
您可以使用XML中的样式来更改单选按钮的未选中和已选中状态的颜色。
<RadioButton
android:id="@+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RadioButtonStyle" />
在style.xml
<style name="RadioButtonStyle" parent="Theme.AppCompat.Light">
<item name="colorAccent">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
</style>
您可以在此样式中设置所需的颜色。
有时候你只需要像这样重写colorControlNormal:
<style name="RadioButtonStyle" parent="AppTheme">
<item name="colorControlNormal">@color/pink</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:textColorSecondary">@color/black</item>
</style>
你会得到一个这样的按钮:
colorControlNormal用于未检查状态,colorAccent用于已检查状态。
你必须使用以下代码:
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radiobutton1"
app:buttonTint="@color/black" />
使用app:buttonTint而不是android:buttonTint和android.support.v7.widget。AppCompatRadioButton代替Radiobutton!
Declare a custom style in your styles.xml file. <style name="MyRadioButton" parent="Theme.AppCompat.Light"> <item name="colorControlNormal">@color/indigo</item> <item name="colorControlActivated">@color/pink</item> </style> Apply this style to your RadioButton via the android:theme attribute. <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Radio Button" android:theme="@style/MyRadioButton"/>
但前提是你的activity扩展了AppCompatActivity。
最简单的方法是在值中更改colourAccent颜色-> colors .xml 但要注意,它也会改变其他东西,如编辑文本光标颜色等。
<颜色名称=“颜色强调”>#75aeff</颜色>
适用于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" />
如果你想为点击和未点击的单选按钮设置不同的颜色,只需使用:
android:buttonTint="@drawable/radiobutton"在radiobutton的XML内容和你的radiobutton. XML文件将是:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#1E88E5"/>
<item android:state_checked="true" android:color="#00e676"/>
<item android:color="#ffffff"/>
</selector>
对于那些想要更改禁用、检查和启用状态的用户,您可以执行以下步骤:
<!-- Or androidX radio button or material design radio button -->
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/black"
android:text="Radiobutton1"
app:buttonTint="@color/radio_button_color" />
然后在color res文件夹中,创建一个名为“radio_button_color.xml”的文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/yellow900" android:state_selected="true" />
<item android:color="@color/yellow800" android:state_checked="true" />
<item android:color="@color/gray800" android:state_enabled="false" />
<item android:color="@color/yellow800" android:state_enabled="true" />
</selector>
我有这个问题。如果你的应用程序有一个黑色背景,你有很多的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>
要以编程方式更改单选按钮的颜色,您可以使用以下方法:
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);
使用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"
/>
创建一个可绘制文件my_compound_button_color_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/text_app_color"/>
<item android:state_checked="true" android:color="@color/text_app_color"/>
<item android:color="@color/gray"/> </selector>
在style.xml文件中添加样式
<style name="AppRadioAppStyle" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:textColor">@drawable/my_compound_button_color_selector</item>
<item name="drawableTint">@drawable/my_compound_button_color_selector</item>
</style>
布局文件
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_horizontal|bottom"
android:background="@drawable/app_border_0"
android:padding="@dimen/_15sdp"
android:text="@string/no"
android:fontFamily="@font/poppins_medium"
style="@style/AppRadioAppStyle"
android:layout_marginStart="@dimen/_10sdp"/>
应该添加android:button="@null"在你的单选按钮
对于不同的颜色根据检查和未检查的状态,请尝试这个-
创建一个颜色资源文件@color/radio_button -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/grey" android:state_enabled="false" />
<item android:color="@color/grey" android:state_checked="false" />
<item android:color="@color/green" android:state_enabled="true" />
<item android:color="@color/green" android:state_checked="true" />
</selector>
然后像这样使用
<androidx.appcompat.widget.AppCompatRadioButton
android:id="@+id/radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/radio_button" />
这个kotlin扩展
fun RadioButton.setLangRadioColor(isCheck: Boolean) { val color = if (isCheck) { intArrayOf( ContextCompat.getColor(rootView.context, R.color.light_red), ContextCompat.getColor(rootView.context, R.color.light_red) ) } else { intArrayOf( ContextCompat.getColor(rootView.context, R.color.sortRadioUnselectColor), ContextCompat.getColor(rootView.context, R.color.sortRadioUnselectColor) ) } val colorStateList = ColorStateList( arrayOf( intArrayOf(-android.R.attr.state_enabled), // disabled intArrayOf(android.R.attr.state_enabled) // enabled ), color ) this.buttonTintList = colorStateList }
推荐文章
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?
- 操作栏和新引入的工具栏有什么不同?