如何自定义Android中单选按钮的默认主题?

5

我希望将单选按钮的默认主题更改为自定义主题,但是它并没有改变主题。请帮帮我。

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>


请查看我下面的答案。 - Suhas Bachewar
6个回答

10

在您的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时


2

最终,我自己找到了答案。

我只需创建自定义布局并将其添加到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
当然,您可以在父级AppTheme中完成此操作,只需执行以下操作: 首先,将MaterialTheme用作父级主题:
<style name="AppTheme" parent="Theme.MaterialComponents.*">

然后在“res/values/styles”中创建您的“RadioButton”样式。
<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"
            />

如果想了解更多关于主题化的细节,请参考主题化单选按钮


1
“radiobutton_drawable.xml”文件末尾缺少“”标签。 此外,您应该对单选按钮进行以下更改-
<RadioButton android:layout_width="wrap_content"
           android:layout_marginTop="20dp"
           android:id="@+id/radio"
           style="@style/radionbutton"
           android:layout_height="wrap_content"/>

0

需要为单选按钮应用个性化样式

<RadioButton
        android:layout_width="wrap_content"
        style="@style/radiobutton_drawable"
        android:layout_height="wrap_content"/>

但是在这里...我已经在列表视图中添加了单选按钮。 - Dharmesh Dhameliya
这是如何使用ListAdapter的示例代码:ListAdapter listAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_single_choice,aryStrLockscreens); listview_lockscreen.setAdapter(listAdapter); - Dharmesh Dhameliya
aryStrLockscreens是字符串数组。 - Dharmesh Dhameliya
我需要使用哪种方法来设置样式? - Dharmesh Dhameliya
为什么不自己创建布局,而不使用默认的布局。以防在充气时不想更改样式。 - Nikunj Sakhrelia
显示剩余2条评论

0
你可以将这段代码添加到你的styles.xml文件中。
<style name="Widget.Styles.Radio.RadioButton" 
    <item name="android:textAppearance">@style/ANY_TYPOGRAPHY_STYLE</item>
</style>

ANY_TYPOGRAPHY_STYLE 例如:

  • 如果您使用的是Material主题,则为TextAppearance.MaterialComponents.Caption
  • 如果您使用的是AppCompat主题,则为TextAppearance.AppCompat.Caption

  • 或者任何你创建的排版风格

示例:

<style name="TextAppearance.TypographyStyles.Body2" parent="TextAppearance.MaterialComponents.Body2">
    <item name="fontFamily">@font/poppins_regular</item>
    <item name="android:fontFamily">@font/poppins_regular</item>
    <item name="android:textSize">14sp</item>
</style>

并将单选按钮样式用作MaterialRadioButtonRadioButton的样式

<com.google.android.material.radiobutton.MaterialRadioButton
            style="@style/Widget.Styles.Radio.RadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEXT"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接