AlertDialog 上缺少按钮 |安卓 7.0 (Nexus 5x) [英] Missing buttons on AlertDialog | Android 7.0 (Nexus 5x)

查看:28
本文介绍了AlertDialog 上缺少按钮 |安卓 7.0 (Nexus 5x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 AlertDialog 但按钮没有显示.仅在 Android 7.0 中看到此问题:

I am trying to create an AlertDialog but the buttons are not showing. Only seeing this issue in Android 7.0:

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    @TargetApi(Build.VERSION_CODES.M)
    public void onDismiss(final DialogInterface dialog) {
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
    }
});
builder.show();

推荐答案

确实似乎需要定义 AlertDialog 主题.上述的另一种方法是在应用程序主题中定义 AlertDialog 主题:

Indeed it seems that AlertDialog theme needs to be defined. An alternative approach to above would be to define AlertDialog theme in Application theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- ... other AppTheme items ... -->
    <item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
</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>
</style>

然后只用Context参数创建AlertDialog.Builder就足够了.

Then it is enough create AlertDialog.Builder only with Context parameter.

注意: 以上似乎只适用于 android.app.AlertDialog.Builder 并且不适用于 AppCompat builder (android.support.v7.app.AlertDialog.Builder,至少从 25.0.1 版本开始).在 AppCompat builder 的情况下,我必须将主题 ID 作为第二个参数传递给 Builder 构造函数以使按钮可见.

Note: The above seems to work only for android.app.AlertDialog.Builder and is not working for AppCompat builder (android.support.v7.app.AlertDialog.Builder, at least as of version 25.0.1). In case of AppCompat builder, I had to pass theme ID as second parameter to Builder constructor to have buttons visible.

这篇关于AlertDialog 上缺少按钮 |安卓 7.0 (Nexus 5x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆