android - 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 样式实现这种设计?
解决方案
以下是如何使用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>
推荐阅读
- javascript - 什么会使 google maps api 集群无法正常工作?
- python - Django Rest Framework 无法解析多部分/表单数据
- android - onSaveInstanceState 之后的 Androidx Navigation IllegalStateException
- laravel - Laravel + Vue 路由器 - 蛞蝓的最佳实践
- node.js - 节点,postgres pg-promise:仅在 azure webapp 容器中泄漏活动句柄
- go - 例行公事:选择真的选择随机案例吗?
- r - 展开 data.frame
- electron - 如何在电子原子的两个扩展监视器的总中心打开 newBrowserWindow
- user-interface - 当数组中元素的值发生变化时,React-Native 更新 UI,而不使用 ForceUpdate
- python - 如果列中的值匹配,则合并熊猫数据框中的计数和行