How to set the theme of an AlertDialog

☆樱花仙子☆ 提交于 2019-12-11 05:19:41

问题


How do I set the theme of an alert to one of the standard Android themes? I want to use Holo Dark, since the popup defaults to Holo Light. My code:

        AlertDialog.Builder confirm = new AlertDialog.Builder(this);
        confirm.setTitle("Confirm");
        confirm.setMessage("Confirm and set delay?");
        confirm.setCancelable(false);
        confirm.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    startDelay();
               }
           });
        confirm.setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
        confirm.show();

回答1:


You can a pass a theme in the constructor.

new AlertDialog.Builder (this, AlertDialog.THEME_HOLO_DARK)



回答2:


Theme with v7 library android.support.v7.app.AlertDialog

android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this,R.attr.alertDialogTheme);

Theme with constructer for android.app.AlertDialog

android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT );

But as per new documentation
This constant(AlertDialog.THEME_HOLO_LIGHT) was deprecated in API level 23. Use Theme_Material_Light_Dialog_Alert .

 android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this,android.R.style.Theme_Material_Light_Dialog_Alert );


来源:https://stackoverflow.com/questions/19869535/how-to-set-the-theme-of-an-alertdialog

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