Ama*_*ngh 141 android android-radiobutton
我想改变单选按钮的圆圈颜色,我无法理解要设置的属性.我所拥有的背景颜色是黑色,因此它变得不可见.我想将圆圈的颜色设置为白色.
Jor*_*any 241
更简单,只需设置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"/>
Run Code Online (Sandbox Code Playgroud)
在你的values/colors.xml中,你的颜色在这种情况下是偏红的:
<color name="your_color">#e75748</color>
Run Code Online (Sandbox Code Playgroud)
结果:

如果你想通过代码(也是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
}
Run Code Online (Sandbox Code Playgroud)
akn*_*nay 154
更新: 1.使用此代替
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rbtn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/primary" />
Run Code Online (Sandbox Code Playgroud)
2.然后将此行添加到父布局或Alt + EnterAndroid Studio中以自动添加
xmlns:app="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>
Run Code Online (Sandbox Code Playgroud)
3.在你的程序中,应该这样打电话.
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);
Run Code Online (Sandbox Code Playgroud)
在API 19测试结果:
有关更多详细信息,请参阅android 参考链接.
小智 74
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/Color" />
Run Code Online (Sandbox Code Playgroud)
Vai*_*rma 50
使用API pre 21以及post 21.
在你的styles.xmlput中:
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
您radio button的xml应该如下所示:
<RadioButton
android:layout_width="wrap_content"
style="@style/radionbutton"
android:checked="false"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
现在,所有你需要做的是让一个radiobutton_drawable.xml在你的drawable folder.以下是您需要添加的内容:
<?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>
Run Code Online (Sandbox Code Playgroud)
你的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>
Run Code Online (Sandbox Code Playgroud)
你的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>
Run Code Online (Sandbox Code Playgroud)
只需用@color/colorAccent您选择的颜色替换即可.
小智 17
你必须使用这段代码:
<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/black" />
Run Code Online (Sandbox Code Playgroud)
使用app:buttonTint代替android:buttonTint也android.support.v7.widget.AppCompatRadioButton代替Radiobutton!
Ras*_*iri 12
适用于API 21
创建自定义样式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>
Run Code Online (Sandbox Code Playgroud)
在布局中使用主题:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RadioButton" />
Run Code Online (Sandbox Code Playgroud)
适用于API 21及更高版本
只需使用buttonTint
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/green" />
Run Code Online (Sandbox Code Playgroud)
小智 11
问题是陈旧但我认为我的答案将有助于人们.您可以使用xml中的样式更改单选按钮的未选中和检查状态的颜色.
<RadioButton
android:id="@+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RadioButtonStyle" />
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
您可以使用此样式设置所需的颜色.
bat*_*eva 11
在您的styles.xml文件中声明自定义样式。
<style name="MyRadioButton" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/indigo</item>
<item name="colorControlActivated">@color/pink</item>
</style>
Run Code Online (Sandbox Code Playgroud)通过android:theme属性将此样式应用于您的RadioButton。
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Radio Button"
android:theme="@style/MyRadioButton"/>
Run Code Online (Sandbox Code Playgroud)
仅当您的活动扩展时 AppCompatActivity
我做了这样的简短介绍(在API 21之前和21上工作)
xml中的单选按钮应如下所示
<RadioButton android:id="@+id/radioid"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:button="@drawable/radiodraw" />
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
必须添加透明颜色以绘制未选中状态;否则它会绘制纯黑色椭圆形。
有时您只需要覆盖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>
Run Code Online (Sandbox Code Playgroud)
你会得到一个这样的按钮:
colorControlNormal用于未选中状态,colorAccent用于选中.
对于那些想要更改禁用、选中和启用状态的人,您可以执行以下步骤:
<!-- 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" />
Run Code Online (Sandbox Code Playgroud)
然后在 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
170294 次 |
| 最近记录: |