如何使AlertDialog透明的布局背景?

发布于 2025-01-31 23:37:19 字数 761 浏览 2 评论 0原文

我正在用Android Studio创建一个应用程序。我创建了一个AlertDialog,其布局的背景设置为此自定义XML:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/connectioncardColor" />
        <corners android:bottomRightRadius="8dp"
            android:bottomLeftRadius="8dp"
            android:topRightRadius="8dp"
            android:topLeftRadius="8dp"/>
        <stroke
            android:color="@color/colorPrimaryDark"
            android:width="1dp"/>
    
    </shape>

背景的角落很好且圆润,但背景后面是另一层白色。因此,角落里有一些白色,如何删除它?

I'm creating an app in Android Studio. I created an AlertDialog with the background of the layout set to this custom xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/connectioncardColor" />
        <corners android:bottomRightRadius="8dp"
            android:bottomLeftRadius="8dp"
            android:topRightRadius="8dp"
            android:topLeftRadius="8dp"/>
        <stroke
            android:color="@color/colorPrimaryDark"
            android:width="1dp"/>
    
    </shape>

The corners of the background are nice and rounded but behind the background is another layer of white. So there is bit of white in the corner, how to remove that?

White in corner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2025-02-07 23:37:19

创建对话框的自定义样式

 <style name="MyDialog" parent="android:Theme.Dialog">        
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
 </style>

,然后在构建器中添加此样式

AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyDialog)

Create your custom style for dialog

 <style name="MyDialog" parent="android:Theme.Dialog">        
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
 </style>

then add this style in builder

AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyDialog)
瞳孔里扚悲伤 2025-02-07 23:37:19

由于我不能仅发布一个链接,因此我也会复制答案,但是我在这里找到了答案:
如何在Android

在可绘制的可绘制中创建XML文件,例如dialog_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/white"/>
    <corners
        android:radius="30dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

将其设置为布局xml中的背景:

android:background="@drawable/dialog_bg"

将对话框的根视图的背景设置为透明,因为Android将您的对话框布局放置在隐藏在遮挡角落的根视图中您的自定义布局。

Java:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Kotlin:

dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

Since I cant just post a link I will copy the answer too, but I found my answer here:
How to make custom dialog with rounded corners in android

Create an XML file in drawable, say dialog_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/white"/>
    <corners
        android:radius="30dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

set it as the background in your layout XML:

android:background="@drawable/dialog_bg"

Set the background of the dialog's root view to transparent, because Android puts your dialog layout within a root view that hides the corners in your custom layout.

Java:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Kotlin:

dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文