Java 使用 Theme.Light.NoTitleBar 显示 Light AlertDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18616011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:32:59  来源:igfitidea点击:

Show Light AlertDialog using Theme.Light.NoTitleBar

javaandroidandroid-alertdialog

提问by Si8

I used the following line in my manifest:

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

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

to have no title bar and display the light version of AlertDialog in my app, like in example: enter image description here

没有标题栏并在我的应用程序中显示 AlertDialog 的精简版本,例如: 在此处输入图片说明

But it's displaying in dark theme still:

但它仍然以深色主题显示:

enter image description here

在此处输入图片说明

My Dialog Java code:

我的对话框 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();

How do I keep the theme light for AlertDialog?

如何保持 AlertDialog 的主题亮起?

采纳答案by James McCracken

The top dialog in your post is a Holo Light themed dialog whereas the bottom one is the older themed dialog. You cannot get a Holo Light themed dialog on versions below Honeycomb. Here is a little snippet I use to select the light theme based on what android version the device is running.

您帖子中的顶部对话框是 Holo Light 主题对话框,而底部是较旧的主题对话框。在 Honeycomb 以下版本中,您无法获得 Holo Light 主题对话框。这是我用来根据设备运行的 android 版本选择浅色主题的一个小片段。

The AlertDialog.Builderwill use the theme of the context it's passed. You can use a ContextThemeWrapperto set this.

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

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);

回答by JavaDM

You have to use an AlertDialog Builder. With it you can set a style to your Dialog. Look the following example: http://pastebin.com/07wyX0V3

你必须使用一个AlertDialog Builder. 有了它,您可以为对话框设置样式。看下面的例子:http: //pastebin.com/07wyX0V3

<style name="popup_theme" parent="@android:style/Theme.Light">
    <item name="android:windowBackground">@color/back_color</item>
    <item name="android:colorBackground">@color/back_color</item>
</style>

回答by h4rd4r7c0r3

You can use something like this when creating your AlertDialog:

你可以在创建你的时候使用这样的东西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.

This code will create Holo Styled AlertDialog in newer versions and normal device based AlertDialog on devices with older version of Android.

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

回答by Sanjeev

If you are using new Material library from google, and want to keep the theme consistent

如果您正在使用来自 google 的新材料库,并希望保持主题一致

implementation 'com.google.android.material:material:1.0.0-beta01'

Simply use this for Light AlertBox

只需将其用于 Light AlertBox

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.Theme_MaterialComponents_Light_Dialog_Alert);

for dark AlertBox

黑暗 AlertBox

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.Theme_MaterialComponents_Dialog_Alert);