我想将单选按钮的默认主题更改为自定义,但它不会更改主题。请帮帮我
styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="android:radioButtonStyle">@style/radionbutton</item>
</style>
<!-- custom style -->
<style name="radionbutton"
parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:button">@drawable/radiobutton_drawable</item>
</style>
radiobutton_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@mipmap/radio_unselected" >
</item>
<item android:state_checked="true" android:drawable="@mipmap/radio_selected">
</item>
答案 0 :(得分:4)
在styles.xml文件中声明自定义样式。
<style name="MyRadioButton" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/indigo</item>
<item name="colorControlActivated">@color/pink</item>
</style>
通过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"/>
仅当您的活动扩展AppCompatActivity
时答案 1 :(得分:1)
最后,我自己找到答案
我只是创建自定义布局并添加到ListAdapter对象
radiobuttonlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="@drawable/radiobutton_drawable"
android:gravity="center_vertical"
android:text="New CheckedTextView"
android:padding="@dimen/dp10"
android:textColor="@color/white" />
Java文件代码
ListAdapter listAdapter = new ArrayAdapter<String> (context,R.layout.radiobuttonlayout,aryStrLockscreens);
listview_lockscreen.setAdapter(listAdapter);
它对我有用。
答案 2 :(得分:0)
需要将个人风格应用于单选按钮
<RadioButton
android:layout_width="wrap_content"
style="@style/radiobutton_drawable"
android:layout_height="wrap_content"/>
答案 3 :(得分:0)
radiobutton_drawable.xml 最后缺少</selector>
标记。
另外,你应该对你的单选按钮 -
<RadioButton android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/radio"
style="@style/radionbutton"
android:layout_height="wrap_content"/>
答案 4 :(得分:0)
当然您可以在父 AppTheme 中执行此操作,只需执行以下操作:
首先,您将使用 MaterialTheme
作为父主题:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
然后在 RadioButton
res/values/styles
样式
<style name="Widget.App.RadioButton" parent="Widget.MaterialComponents.CompoundButton.RadioButton">
<item name="buttonTint">@color/white</item>
//put any thing here
</style>
将其添加到父级 AppTheme
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<item name="radioButtonStyle">@style/Widget.App.RadioButton</item>
</style>
然后在您的布局中使用它,例如:
<com.google.android.material.radiobutton.MaterialRadioButton
android:id="@+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi"
/>
有关主题的更多详细信息 Theming RadioButton