首页 > 解决方案 > Android:AlertDialog 中 RadioButton 的自定义样式

问题描述

我正在像这样在 AlertDialog 中构建 RadioGroup:

val items = listOf("1", "2")

AlertDialog.Builder(activity, R.style.MyStyle)
        .setSingleChoiceItems(items.toTypedArray(), 0) { dialog, index ->
            dialog.dismiss().   
        }
        .show()

我想要的结果是让这个 AlertDialog 看起来像这样:

在此处输入图像描述

因此,通过样式,我需要删除实际的 RadioButton 可绘制对象,然后让当前选定的 RadioButton 具有灰色背景。

如何使用 Android 样式实现这种设计?

标签: androidradio-buttonandroid-alertdialogandroid-styles

解决方案


以下是如何使用AlertDialog.Builder.

styles.xml

<style name="MyStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:listChoiceIndicatorSingle">@null</item>
    <item name="android:background">@drawable/radio_button_background_selector</item>
</style>

radio_button_background_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@color/hig_light_grey" />
</selector>

推荐阅读