首页 > 解决方案 > 无法使我的 ConstraintLayout 半透明/透明

问题描述

我有一个 ConstraintLayout,我在我的应用程序中用作弹出窗口,我希望这个弹出窗口是半透明的。

我尝试过使用#33000000 之类的颜色,它应该是半透明的黑色,但它显示为浅灰色。如果我将背景颜色设置为“@android:color/transparent”并且它变为白色,那么我认为问题是我喜欢默认的白色背景。

此外,当我设置圆角时,我看到我的 ConstrinatLayout 后面的白色背景带有圆角。

这是我的 ConstraintLayout 代码。我有几个 TextViews 和一个图像里面。

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="350dp"
android:layout_height="475dp"
android:background="#33000000"
android:padding="10dp"
android:layout_gravity="center">

如果需要更多代码或详细信息,请告诉我。

以下屏幕截图是半径=50dp 和背景颜色=#B3808080。

弹出屏幕截图

标签: androidxmllayoutbackgroundtransparent

解决方案


在 Base 主题标签下的 style.xml 中设置 WindowdisablePreview

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:windowDisablePreview">true</item>

    </style>

然后在对话框方法中设置背景透明

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

推荐阅读