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

How to customize default theme of radio button in android?

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

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>

需要对单选按钮应用单独的样式

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

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"/>

终于自己找到了答案

我只是创建自定义布局并添加到 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);

对我有用。

在您的 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"/>

仅当您的 activity 扩展了 AppCompatActivity

当然可以在父 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"
            />

有关主题的更多详细信息Theming RadioButton

您可以将此添加到您的 styles.xml

<style name="Widget.Styles.Radio.RadioButton" 
    <item name="android:textAppearance">@style/ANY_TYPOGRAPHY_STYLE</item>
</style>

ANY_TYPOGRAPHY_STYLE例如:

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

  • 或您创建的任何排版样式

示例:

<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"/>