问题
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