【问题标题】:Alert dialog buttons are too close警报对话框按钮太近
【发布时间】:2018-10-09 12:43:38
【问题描述】:

我看到这个东西与警报对话框的按钮接触(它们之间没有空格)。

无论使用什么主题都会发生这种情况。代码:

builder.setTitle(R.string.sign_in_title);
builder.setCancelable(false)
        .setPositiveButton(R.string.sign_in, (dialog, id) -> {
            //Todo
        })
        .setNegativeButton(R.string.cancel, (dialog, id) -> dialog.cancel());
builder.create().show();

应用主题继承:

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

我尝试仅使用 Alert.Dialog 构建一个最小项目设置,但我看到了同样的情况..

任何想法发生了什么或如何解决它?

编辑:我知道我可以更改警报的主题以产生不同的结果,但这也意味着我的应用程序中不会有统一的按钮(即绿色填充白色文本)

【问题讨论】:

  • 问题在于混合使用 android.app 和 androidx.appcompact.app 对话框,因此修复方法是仅使用 androidx.appcompact.app。

标签: android user-interface themes material-design


【解决方案1】:

只需从支持的库中更改 AlertDialog 的导入类:

import androidx.appcompat.app.AlertDialog;

而不是

import android.app.AlertDialog;

谢谢。

【讨论】:

  • 我认为这更像是一个幸运的巧合,对我不起作用。您应该尽可能使用 androidx 组件,所以至少这个答案没有误导性。
【解决方案2】:

为您的 alertDialog 使用主题,

AlertDialog.Builder(context, R.style.Base_Theme_AppCompat_Light_Dialog);

【讨论】:

    【解决方案3】:

    适合我

      <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="windowNoTitle">true</item>
        <item name="android:windowCloseOnTouchOutside">false</item>
        <item name="android:layout_margin">2dp</item>
    

    只需调用“DialogTheme”即可在您的对话活动中使用

    【讨论】:

    • 添加关闭样式 并将其粘贴到样式中后,您必须在对话框中使用此样式 val builder = AlertDialog.Builder(requireContext(), R.style.DialogTheme)
    【解决方案4】:

    用这个替换你的风格

    <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
            <item name="windowNoTitle">true</item>
            <item name="android:windowCloseOnTouchOutside">false</item>
            <item name="android:windowBackground">@color/transparent</item>
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    

    【讨论】:

    猜你喜欢
    • 2012-01-03
    • 2019-03-20
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多