Android 为对话框标题栏设置背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2391652/
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-20 05:40:55  来源:igfitidea点击:

Set background color for Dialog titlebar?

android

提问by user246114

I'm creating a class derived from Dialog. The titlebar of the dialog looks really nice, it's a dark grey color that is somewhat transparent. Is there a way to set the color used for the background of the titlebar? The grey is cool, but I would like to set it to some custom color. I don't think this is possible, I think I'd need to supply my own stretchable background dialog resource. Is that right?

我正在创建一个派生自 Dialog 的类。对话框的标题栏看起来非常漂亮,它是一种有点透明的深灰色。有没有办法设置用于标题栏背景的颜色?灰色很酷,但我想将其设置为某种自定义颜色。我认为这是不可能的,我想我需要提供我自己的可拉伸背景对话框资源。那正确吗?

Thanks

谢谢

回答by user1730706

Use the below code:

使用以下代码:

final Dialog mailDialog = new Dialog(MainActivity.this);
mailDialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_box);

And create a custom dialog box xml in drawable folder as below:

并在 drawable 文件夹中创建一个自定义对话框 xml,如下所示:

dialog_box.xml

dialog_box.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

    <gradient
        android:angle="-90"
        android:centerColor="#660D1E4A"
        android:endColor="#66011444"
        android:startColor="#66505E7F"
        android:type="linear"
         />

    <stroke
        android:dashGap="0dp"
        android:dashWidth="0dp"
        android:width="1dp"
        android:color="#ffffffff" />

</shape>

Hope this helps you.

希望这对你有帮助。

回答by Demonofloom

You can use:

您可以使用:

this.getWindow().setBackgroundDrawableResource(R.color.blue);

That will set the entire window color including the titlebar.

这将设置包括标题栏在内的整个窗口颜色。

You can then change the background color for the layout of the dialog which is everything but the titlebar to whatever you like and the title bar will remain blue.

然后,您可以将对话框布局的背景颜色更改为您喜欢的任何内容,但标题栏将保持蓝色。

回答by Bernard Banta

The best way is to use a custom dialog where you can then customize its look.

最好的方法是使用自定义对话框,然后您可以在其中自定义其外观。

Have a look at Set AlertBox Title Bar Background Colorand this http://developer.android.com/guide/topics/ui/dialogs.htmlon how to go about it.

看看设置警报框标题栏背景颜色和这个http://developer.android.com/guide/topics/ui/dialogs.html关于如何去做。

回答by arun-r

dialog = new Dialog(this); // your dialog
dialog.getWindow().setTitleColor(R.color.blue_background);

回答by mattfred

The background color of the title section can easily be set this way:

标题部分的背景颜色可以通过这种方式轻松设置:

int titleLayoutId = dialog.getContext().getResources().
    getIdentifier("topPanel", "id", "android"); 

    LinearLayout layout = (LinearLayout) dialog.findViewById(titleLayoutId);
    if (layout != null) {
        layout.setBackgroundColor(yourColor);
    }

This eliminates the need to create a custom layout.

这消除了创建自定义布局的需要。

回答by TareQ

dialog.getWindow().setTitleColor(getResources().getColor(R.color.blue));