【问题标题】:AlertDialog single choice item paddingAlertDialog 单选项填充
【发布时间】:2020-07-24 17:54:52
【问题描述】:

我想在 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>

那么使用styleActivity 代码非常简单:

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

            dialog.dismiss()
        }
        .show()

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

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

【问题讨论】:

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


    【解决方案1】:

    您将覆盖整个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)吧。

    带有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>
    

    【讨论】:

    • 在添加这 2 个 style 和 1 个 layout 资源后,当我呈现对话框时,它看起来不像 layout 资源的 CheckedTextView 正在使用。它似乎默认为默认的 Android 布局。我用来在活动中呈现对话框的代码在原始问题中,如果相关的话。
    • 你能分享你的活动代码吗?我的代码没有获取CheckedTextView 布局文件。
    • @freezefry 没什么特别的。只需AlertDialog.Builder(this, R.style.customLMultiItem)MaterialAlertDialogBuilder(this,R.style.customLMultiItem)
    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多