当前位置:  开发笔记 > 编程语言 > 正文

使用Theme.Light.NoTitleBar显示Light AlertDialog

如何解决《使用Theme.Light.NoTitleBar显示LightAlertDialog》经验,为你挑选了2个好方法。

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

android:theme="@android:style/Theme.Light.NoTitleBar"

没有标题栏并在我的应用程序中显示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();

如何保持AlertDialog的主题灯?



1> James McCrac..:

帖子中的顶部对话框是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);



2> hardartcore..:

您可以在创建时使用以下内容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.

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

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有