【问题标题】:how to make center MaterialAlertDialogBuilder alignment title, message & button如何使中心 MaterialAlertDialogBu​​ilder 对齐标题、消息和按钮
【发布时间】:2020-06-24 19:22:20
【问题描述】:

如何使标题、消息和按钮居中对齐

MaterialAlertDialogBuilder(requireContext())
                .setTitle("Message")
                .setMessage("This is info message.")
                .setPositiveButton("Ok") { dialog, which ->
             
                }
                
                .show()

【问题讨论】:

标签: java android kotlin android-alertdialog material-components-android


【解决方案1】:

对于标题和您可以使用的消息:

    <style name="MaterialAlertDialog__Center" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="materialAlertDialogTitlePanelStyle">@style/Title.Panel.Center</item>
        <item name="materialAlertDialogBodyTextStyle">@style/Message.Center</item>
    </style>

    <style name="Title.Panel.Center" parent="MaterialAlertDialog.MaterialComponents.Title.Panel">
        <item name="android:gravity">center_horizontal</item>
    </style>

    <style name="Message.Center" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
        <item name="android:gravity">center_horizontal</item>
    </style>

与:

MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialog_Center)
                .setTitle("Message")
                ..
                .show()

【讨论】:

  • 它正在工作,谢谢你,你对中心对齐按钮以及自定义字体(标题、副标题和按钮)有任何想法
  • @HariShankarS 关于对齐我不知道样式解决方案。没有公开的方法可以做到这一点。关于按钮的文字、字体、颜色可以查看这个答案:stackoverflow.com/a/58043299/2016562
【解决方案2】:

您可以创建自己的布局并将其添加到对话框中

val inflater=LayoutInflater.from(requireContext())
//Getting the custom view
val view=inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT,null)
//Obtaining the components
val title=view.findViewById(R.id.YOUR_CUSTOM_TILE)
title.text="Message"
val button=view.findViewById(R.id.YOUR_CUSTOM_BUTTON)
button.setOnCLickListener{
    //onClick
    }
val dialog=MaterialAlertDialogBuilder(requireContext()).setView(view).create()
dialog.show()

【讨论】:

  • 感谢您的建议,我正在寻找基于样式的解决方案(使用自定义字体居中对齐:材质对话框标题、副标题和按钮)。
【解决方案3】:

要使标题和消息居中,您只需在主题中进行更改

<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Centered</item>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2021-05-14
    相关资源
    最近更新 更多