警报对话框按钮间距太近。

6

我看到一个关于警告对话框按钮触摸的问题(它们之间没有空格)。

enter image description here

无论使用哪个主题,都会发生这种情况。 代码:
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构建最小项目设置,结果发现出现了相同的问题。
你有什么想法或如何解决这个问题吗?
编辑:我知道我可以更改Alert的主题以产生不同的结果,但这也意味着我的应用程序中的按钮将不统一(即绿色填充白色文本)。

这个问题在于混合使用了android.app和androidx.appcompact.app对话框,因此解决方法是只使用androidx.appcompact.app。 - sinek
4个回答

17

只需将导入的 AlertDialog 类替换为支持库中的对应类:

import androidx.appcompat.app.AlertDialog;

取代

import android.app.AlertDialog;

谢谢。


我认为这更像是一个幸运的巧合,对我没有用。尽可能使用androidx组件,这样至少这个答案不会误导。 - kolboc

1
使用主题来设置你的AlertDialog。
AlertDialog.Builder(context, R.style.Base_Theme_AppCompat_Light_Dialog);

1

对我来说工作得很好

  <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”即可。

1
添加闭合样式 </style>,并将其粘贴到样式中后,您必须在对话框中使用此样式。val builder = AlertDialog.Builder(requireContext(), R.style.DialogTheme) - Waheed Nazir

0

使用这个替换你的样式

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

我明白,但这似乎不完全符合指南:https://material.io/develop/android/docs/getting-started/,是吗? - sinek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接