Missing buttons on AlertDialog | Android 7.0 (Nexus 5x)

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

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();

回答1:

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

  

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

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.



回答2:

So it turns out on Android 7.0 you have to provide a theme. At least, that's what I had to do.

           final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme);


回答3:

What worked for me was in styles.xml:

and

and in your program:

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme);


回答4:

You can create a custom theme for Alert Dialog, and set alertDialogTheme in your app theme.

    

and finally, set the custom created theme to alertDialogTheme in Application Theme:

Tested for android.support.v7.app.AlertDialog



回答5:

You need use a theme, like this:

Android.App.AlertDialog        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!