使用Theme.Light.NoTitleBar显示Light AlertDialog

Si8*_*Si8 9 java android android-alertdialog

我在清单中使用了以下行:

android:theme="@android:style/Theme.Light.NoTitleBar"
Run Code Online (Sandbox Code Playgroud)

没有标题栏并在我的应用程序中显示AlertDialog的简易版本,例如: 在此输入图像描述

但它仍以黑暗主题展示:

在此输入图像描述

我的Dialog Java代码:

    new AlertDialog.Builder(FreeDraw.this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("Clear Drawing?")
    .setMessage("Do you want to clear the drawing board?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
            startActivity(getIntent());  
        }
    })
    .setNegativeButton("No", null)
    .show();
Run Code Online (Sandbox Code Playgroud)

如何保持AlertDialog的主题灯?

Jam*_*ken 28

帖子中的顶部对话框是Holo Light主题对话框,而底部对话框是较旧的主题对话框.你不能在Honeycomb下面的版本上获得Holo Light主题对话框.这是我用来根据设备运行的android版本选择灯光主题的小片段.

AlertDialog.Builder会用它传递的上下文的主题.您可以使用a ContextThemeWrapper来设置它.

ContextThemeWrapper themedContext;
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
    themedContext = new ContextThemeWrapper( FreeDraw.this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar );
}
else {
    themedContext = new ContextThemeWrapper( FreeDraw.this, android.R.style.Theme_Light_NoTitleBar );
}
AlertDialog.Builder builder = new AlertDialog.Builder(themedContext);
Run Code Online (Sandbox Code Playgroud)


har*_*ore 14

您可以在创建时使用以下内容AlertDialog:

AlertDialog.Builder builder = null;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
    builder = new AlertDialog.Builder(BaseActivity.this);
} else {
    builder = new AlertDialog.Builder(BaseActivity.this, AlertDialog.THEME_HOLO_LIGHT);
}
// ... do your other stuff.
Run Code Online (Sandbox Code Playgroud)

此代码将在较新版本中创建Holo Styled AlertDialog,在具有旧版Android的设备上创建基于普通设备的AlertDialog.