android - AlertDialog 单选项填充

标签 android android-layout android-alertdialog android-theme android-styles

我想在 AlertDialog 中的每个选项之间添加顶部和底部填充。我尝试使用此 style 资源来实现此目标:

<style name="CustomDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:listChoiceIndicatorSingle">@null</item>
    <item name="android:background">@drawable/radio_button_background_selector</item>
    <item name="android:paddingTop">17dp</item>
    <item name="android:paddingBottom">17dp</item>
</style>

然后使用此样式Activity代码非常简单:

AlertDialog.Builder(this, R.style.CustomDialog)
        .setSingleChoiceItems(options, currentSelectedIndex) { dialog, index ->
            if (index != currentSelectedIndex) {
                // do something
            }

            dialog.dismiss()
        }
        .show()

填充按预期工作,只是它还在 DialogFragment 的顶部和底部添加了巨大的边距。

如何在每个单项选择项之间添加填充而不产生这种意外后果?

enter image description here

最佳答案

您正在覆盖整个 Dialog 中的 paddingToppaddingBottom

通过 AppCompat 主题,您可以使用如下主题叠加层:

<style name="customLMultiItem" parent="@style/ThemeOverlay.AppCompat.Dialog.Alert">
    <!-- if you want to remove the indicator in each row -->
    <item name="android:listChoiceIndicatorSingle">@null</item> 
    <item name="alertDialogStyle">@style/my.AlertDialog</item>
</style>

<style name="my.AlertDialog" parent="AlertDialog.AppCompat">
    <!-- to use a custom layout for the singlechoice -->
    <item name="singleChoiceItemLayout">@layout/my_dialog_singlechoice</item>
</style>

通过这种方式,您可以使用自定义布局(从原始布局复制+填充):

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@android:id/text1"
                 android:paddingTop="XXdp"
                 android:paddingBottom="Xdp"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:minHeight="?attr/listPreferredItemHeightSmall"
                 android:textAppearance="?android:attr/textAppearanceMedium"
                 android:textColor="?attr/textColorAlertDialogListItem"
                 android:gravity="center_vertical"
                 android:paddingLeft="@dimen/abc_select_dialog_padding_start_material"
                 android:paddingRight="?attr/dialogPreferredPadding"
                 android:paddingStart="@dimen/abc_select_dialog_padding_start_material"
                 android:paddingEnd="?attr/dialogPreferredPadding"
                 android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
                 android:drawableStart="?android:attr/listChoiceIndicatorSingle"
                 android:drawablePadding="20dp"
                 android:ellipsize="marquee" />

然后只需使用AlertDialog.Builder(this, R.style.customLMultiItem)

enter image description here

使用MaterialComponents主题:

<style name="customLMultiItem" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="android:listChoiceIndicatorSingle">@null</item>
    <item name="alertDialogStyle">@style/my.MaterialAlertDialog.MaterialComponents</item>
</style>

<style name="my.MaterialAlertDialog.MaterialComponents" parent="MaterialAlertDialog.MaterialComponents">
    <item name="singleChoiceItemLayout">@layout/my_dialog_singlechoice</item>
</style>

enter image description here

关于android - AlertDialog 单选项填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63079023/

相关文章:

php - 将经度、纬度和电话号码发送到 mysql 数据库 - Android

java - 在游戏 View 下添加 admob

c# - Xamarin 商店信息

java - 发送短信之前显示确认对话框

android - 如何在后台服务中使用alertDialog

android - android sherlock actionbar 与 iOS 中的 UINavigationBar 相同吗?

java.lang.NoSuchMethodError : No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classe

java - 设置多方向 - Android

android-layout - 更改android editText高亮颜色

android - 带有 Theme.Sherlock.Light 的 AlertDialog 上深色背景上的深色文本