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

2023-09-07 10:09:11 作者:复制

我创建的对话框派生的类。对话框的标题栏看起来真的不错,这是一个暗灰色的颜色,有点透明。有没有一种方法来设置用于标题栏的背景颜色?灰色是很酷,但我想将它设置为一些自定义颜色。我不认为这是可能的,我想我需要提供自己的可拉伸的背景对话框资源。是吗?

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?

感谢

推荐答案

使用下面的code:

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

和创建文件夹,绘制如下自定义对话框的xml:

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

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.