【问题标题】:How can I make the background of my dialog in android transparent?如何使android中对话框的背景透明?
【发布时间】:2022-01-23 22:58:16
【问题描述】:

我正在尝试使对话框的背景透明。我希望进度条显示在我的活动前面。不幸的是,背景始终是白色的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loading_cont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:backgroundTint="#00000000"
android:background="@android:color/transparent"
>

<RelativeLayout
    android:id="@+id/loading_dialog_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#00000000"
    android:background="@android:color/transparent"
    >

    <ProgressBar
        android:id="@+id/login_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true" >
    </ProgressBar>

</RelativeLayout>

【问题讨论】:

    标签: android android-studio android-layout android-dialogfragment android-dialog


    【解决方案1】:

    创建一个名为 transparent.xml 的新 Drawable Resource file

    transparent.xml

    <shape 
          android:shape="rectangle"
          xmlns:android="http://schemas.android.com/apk/res/android">
    </shape>
    

    现在设置android:background="@drawable/transparent"

    【讨论】:

    • 我试过了。我在 LinearLayout、Relativelayout 和 ProgressBar 中设置了 android:background="@drawable/transparent"。它仍然保持白色。
    【解决方案2】:

    在初始化对话框的位置添加此代码

    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    

    或者您也可以添加此代码

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

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多