先上代码
class AlertClickListener implements OnClickListener { @Override public void onClick(View v) { boolean checkedd = (Boolean) SpUtils.get(SettingActivity.this, "night_day", false); Builder builder; if (checkedd) { builder = new AlertDialog.Builder(SettingActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK); }else { builder = new AlertDialog.Builder(SettingActivity.this); } builder.setTitle("选择区域"); builder.setItems(areas, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Log.d("yangxf", " which = "+which); switch (which) { case 0: Log.d("yangxf", " case 1 which = " + which); SpUtils.put(SettingActivity.this, "fontSize", 1); break; case 1: Log.d("yangxf", " case 2 which = " + which); SpUtils.put(SettingActivity.this, "fontSize", 2); break; case 2: Log.d("yangxf", " case 3 which = " + which); SpUtils.put(SettingActivity.this, "fontSize", 3); break; default: break; } Intent intent = getIntent(); overridePendingTransition(0, 0); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(intent); dialog.dismiss(); } }).show(); } } 这是核心部分代码,关于夜间模式部分请查看相关的帖子 boolean checkedd = (Boolean) SpUtils.get(SettingActivity.this, "night_day", false);这里应用了Sharepreference记录了夜间模式还是日间模式的切换
builder = new AlertDialog.Builder(SettingActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);这里是设置了dialog的默认背景样式相关的默认样式有
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_TRADITIONAL);//传统主题 AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_HOLO_DARK);//使用深色背景 主题 AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_HOLO_LIGHT);//使用浅色背景 主题 AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);//使用默认设备 深色主题 AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);//使用默认设备 浅 色主题 //自定义布局 View layout = getActivity().getLayoutInflater().inflate(R.layout.mydialog,null); AlertDialog.Builder dialog = new AlertDialog.Builder(DialogActivity.this,AlertDialog. THEME_DEVICE_DEFAULT_DARK); final AlertDialog dialog = builder.create(); dialog.setView(layout); //透明 Window window = dialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.alpha = 0.9f; window.setAttributes(lp); 。。。 dialog.show();