我想在我的一个项目中改变RadioButton的圆圈的颜色,但我不知道该设置哪个属性。背景颜色是黑色的,所以它是看不见的。我想把圆圈的颜色设置为白色。
当前回答
要以编程方式更改单选按钮的颜色,您可以使用以下方法:
yourradio button name.buttonDrawable?.setColorFilter(Color.parseColor( color_value), PorterDuff.Mode.SRC_ATOP)
其他回答
@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的答案是正确的。
设置buttonTint属性。例如,android:buttonTint="#99FF33"。
更新:
用这个代替 < 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参考链接。
如果你有android:buttonTint,它不会工作,你必须改变它到app:buttonTint。 在升级到androidx之后,我不得不这样做。
只需在<RadioButton>标签上使用android:buttonTint="@color/colorPrimary"属性。
推荐文章
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?