向AlertDialog添加边框

时间:2021-04-14 18:54:52

I am trying to add a border to an alert dialog. I am hoping to make it look like this:

我正在尝试向警报对话框添加边框。我希望看起来像这样:

向AlertDialog添加边框

The best solution I have found thus far is to use a nine patch drawable as the background for the dialog.

到目前为止,我发现的最佳解决方案是使用九个可绘制的补丁作为对话框的背景。

The problem with this is that I have not found a way to make a nine patch background that actually gives the dialog a consistent white line around it. This has been my best attempt thus far (sorry it is a little hard to see...) :

这个问题是我还没有找到一种方法来制作一个九个补丁背景,它实际上为对话框提供了一致的白线。这是迄今为止我最好的尝试(对不起,有点难以看到......):

向AlertDialog添加边框

The problem is that this produces a dialog like this:

问题是,这会产生如下对话框:

向AlertDialog添加边框

The problems here are twofold; the lines at the sides are way too thick, and the lines at the top are kind of faded by the shadow.

这里的问题是双重的;两侧的线条太粗,顶部的线条有点被阴影褪色。

My only ideas are to either find a working nine patch that gives a consistently thick border, or to find a way to get the 'main layout' of the alert dialog, so I can add a padding to that directly.

我唯一的想法是找到一个工作九个补丁,给出一个厚边框,或找到一种方法来获得警报对话框的“主要布局”,所以我可以直接添加填充。

What is the best way to go about setting up a border on an Alert Dialog like this?

在这样的警报对话框上设置边框的最佳方法是什么?

2 个解决方案

#1


The answer by questioner was very close to what I wanted, but it still left a big black line around the dialog that I was not interested in.

提问者的回答非常接近我想要的,但它仍然在我不感兴趣的对话框周围留下了一条很大的黑线。

I deleted one of the layers from the layer list, and customised the padding on the white border. I also did not set the background to transparent as suggested would be a good idea in the comments.

我从图层列表中删除了其中一个图层,并在白色边框上自定义了填充。我也没有将背景设置为透明,因为建议在评论中是个好主意。

The shape the is inside the item is to give the background the same curved edges on a dialog (if you look really closely on 3.0+, you can see a few pixels of round corners).

项目内部的形状是在对话框中为背景提供相同的弯曲边缘(如果你仔细观察3.0+,你可以看到一些圆角像素)。

In a dialog_border.xml file in the drawable folder, I had this:

在drawable文件夹中的dialog_border.xml文件中,我有这个:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="6dp"
        android:left="13dp"
        android:right="13dp"
        android:bottom="6dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/offWhite" />

            <corners
                android:radius="3dp"/>
        </shape>
    </item>
</layer-list>

And in my style I had this:

按照我的风格,我有这个:

<style name="DialogTheme" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@drawable/dialog_border</item>
</style>

Although it could probably be set programatically rather than in the style if necessary.

虽然它可能是以编程方式设置的,而不是在必要时以风格设置。

#2


In your dialog's xml set following background for top level view:

在对话框的xml集中,后面是*视图的背景:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white"/>
    <item
        android:bottom="1dp"
        android:drawable="@color/black"
        android:left="1dp"
        android:right="2dp"
        android:top="2dp">
    </item>
</layer-list>

You can customise border width on each side.

您可以自定义每侧的边框宽度。

#1


The answer by questioner was very close to what I wanted, but it still left a big black line around the dialog that I was not interested in.

提问者的回答非常接近我想要的,但它仍然在我不感兴趣的对话框周围留下了一条很大的黑线。

I deleted one of the layers from the layer list, and customised the padding on the white border. I also did not set the background to transparent as suggested would be a good idea in the comments.

我从图层列表中删除了其中一个图层,并在白色边框上自定义了填充。我也没有将背景设置为透明,因为建议在评论中是个好主意。

The shape the is inside the item is to give the background the same curved edges on a dialog (if you look really closely on 3.0+, you can see a few pixels of round corners).

项目内部的形状是在对话框中为背景提供相同的弯曲边缘(如果你仔细观察3.0+,你可以看到一些圆角像素)。

In a dialog_border.xml file in the drawable folder, I had this:

在drawable文件夹中的dialog_border.xml文件中,我有这个:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="6dp"
        android:left="13dp"
        android:right="13dp"
        android:bottom="6dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/offWhite" />

            <corners
                android:radius="3dp"/>
        </shape>
    </item>
</layer-list>

And in my style I had this:

按照我的风格,我有这个:

<style name="DialogTheme" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@drawable/dialog_border</item>
</style>

Although it could probably be set programatically rather than in the style if necessary.

虽然它可能是以编程方式设置的,而不是在必要时以风格设置。

#2


In your dialog's xml set following background for top level view:

在对话框的xml集中,后面是*视图的背景:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white"/>
    <item
        android:bottom="1dp"
        android:drawable="@color/black"
        android:left="1dp"
        android:right="2dp"
        android:top="2dp">
    </item>
</layer-list>

You can customise border width on each side.

您可以自定义每侧的边框宽度。