【问题标题】:Missing Dialog Buttons under Android 7.1.1Android 7.1.1 下缺少对话框按钮
【发布时间】:2017-01-14 21:21:41
【问题描述】:

这是在我的应用程序中显示的 AlertDialog 的图片。 它应该有一个拒绝和一个接受按钮。

如你所见,它没有:

我无法重现此错误,因为我没有搭载 Android 7.1 的手机。图片是在 Google Pixel 上拍摄的,然后发送给我。

测试此应用程序的所有其他 Android 版本均未遇到此错误。 (版本 4.1、6.0.1)

下面是创建对话框的方法代码:

  /**
 * Creates a 2 options dialog.
 * @param context
 * @param title headline of the dialog
 * @param message main text of the dialog
 * @param accept listener for the accept button
 * @param deny listener for deny button
 * @param acceptText text of the positive answer button
 * @param denyText text of the negative answer button
 * @param cancelable weather a click to anywhere but the presented buttons dismisses the dialog
 * @return a created dialog instance. To display it call show()
 */
public static AlertDialog createAcceptDenyDialog(Context context,
                                                 String title, String message, String acceptText,
                                                 String denyText, boolean cancelable,
                                                 DialogInterface.OnClickListener accept,
                                                 DialogInterface.OnClickListener deny,
                                                 DialogInterface.OnDismissListener dismiss){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton(acceptText, accept)
            .setNegativeButton(denyText, deny)
            .setCancelable(cancelable)
            .setOnDismissListener(dismiss);
    return alertDialog.create();
}

这是导致显示对话框的代码:

public void showRequestErrorRetryDialog(String title, String message) {
    Dialog dialog  = DialogFactory.createAcceptDenyDialog(this
            , title
            , message
            , getString(R.string.retry_button)
            , getString(R.string.abort_button)
            , true
            , (dialogInterface, i) -> {
                onStartServerCommunication();
                showProgressOverlay();
            }
            , null
            , null);
    dialog.show();
}

如你所见,我使用的是 retrolambda。

有人知道会发生什么吗?

【问题讨论】:

  • 也许您传递了错误的上下文?您的方法中的this 是什么?在Android 7中对我有用的是new AlertDialog.Builder(context).attributes.show()..(属性都是.setTitle().setPositiveButton()等方法。
  • 方法 showRequestErrorRetryDialog 从应该显示对话框的活动中调用。因此,“this”是活动的上下文
  • 这是活动呢?为什么不使用 getApplicationContext() ?
  • 你需要明确设置主题,看这里:stackoverflow.com/questions/39621606/…

标签: android android-7.1-nougat


【解决方案1】:

对我有用的解决方案是在我的 style.xml 中添加以下几行:

// your main style
<style name="YourStyleName" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
    <item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>

// dialog style
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="buttonBarButtonStyle">@style/DialogButtonStyle</item>
</style>

// button's dialog style
<style name="DialogButtonStyle" parent="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">@color/colorPrimary</item>
</style>

效果很好,希望对大家有帮助。

【讨论】:

  • 如果你使用Android.Support.V7.App.AlertDialog,它就可以工作。对我来说定义以下样式就足够了&lt;style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"&gt; &lt;item name="android:textColor"&gt;@color/primaryColor&lt;/item&gt; &lt;/style&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2022-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多