首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AlertDialog单选项目填充

AlertDialog单选项目填充
EN

Stack Overflow用户
提问于 2020-07-25 01:54:53
回答 1查看 286关注 0票数 2

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

代码语言:javascript
运行
复制
<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代码就非常简单了:

代码语言:javascript
运行
复制
AlertDialog.Builder(this, R.style.CustomDialog)
        .setSingleChoiceItems(options, currentSelectedIndex) { dialog, index ->
            if (index != currentSelectedIndex) {
                // do something
            }

            dialog.dismiss()
        }
        .show()

除了在DialogFragment的顶部和底部添加了巨大的边距之外,填充的工作方式与预期的一样。

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

EN

回答 1

Stack Overflow用户

发布于 2020-07-25 02:16:26

您正在覆盖整个Dialog中的paddingToppaddingBottom

对于AppCompat主题,您可以使用如下主题覆盖:

代码语言:javascript
运行
复制
<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>

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

代码语言:javascript
运行
复制
<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为主题:

代码语言:javascript
运行
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63079023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档