本文介绍了AlertDialog上的缺少按钮| Android 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主题.上面的一种替代方法是在Application主题中定义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构建器(android.support.v7.app.AlertDialog.Builder,至少从 25.0.1 版本开始) .对于AppCompat构建器,我必须将主题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上的缺少按钮| Android 7.0(Nexus 5x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:54