Java 如何制作对话片段';谁的背景不可见?

Java 如何制作对话片段';谁的背景不可见?,java,android,android-studio,Java,Android,Android Studio,我在Flow的方法中使用YouTube编码来创建自定义对话框。我一整天都在努力使对话框的背景透明。我使用了我在网上找到的每一种方法。不工作 事情是这样的: 首先是对话框布局layout\u Dialog.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com

我在Flow的方法中使用YouTube编码来创建自定义对话框。我一整天都在努力使对话框的背景透明。我使用了我在网上找到的每一种方法。不工作

事情是这样的:

  • 首先是对话框布局layout\u Dialog.xml

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:background="@drawable/dialog_background">
    
            <!-- The contents of the Dialog go here -->
    
        </RelativeLayout>
    
        <ImageView
            android:id="@+id/iconImageView2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="0dp"
            android:background="?attr/actionBarItemBackground"
            android:scaleType="fitCenter"
            app:srcCompat="@android:mipmap/sym_def_app_icon" />
    
    </RelativeLayout>
    
  • 下面是从主活动调用的对话框:

    DialogBrightness dialogBrightness = new DialogBrightness();
    dialogBrightness.show(getSupportFragmentManager(), "Brightness Dialog");
    
以下是对话框的显示方式:

我想让上面的白色部分看不见。什么都不管用

试试这个:

将父布局的背景色设置为:

android:background="@android:color/transparent"
像这样:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:background="@drawable/dialog_background">

    <!-- The contents of the Dialog go here -->

</RelativeLayout>

<ImageView
    android:id="@+id/iconImageView2"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="0dp"
    android:background="?attr/actionBarItemBackground"
    android:scaleType="fitCenter"
    app:srcCompat="@android:mipmap/sym_def_app_icon" />

</RelativeLayout>

试试这个:

在OnCreate对话框中输入以下代码:

    // set the dialog background to transparent
    getDialog().getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

    // remove background dim 
    getDialog().getWindow().setDimAmount(0);

您可以按如下方式设计布局。有一个额外的布局,但在对话框的情况下,它会有所帮助


我已经试过了:(.它显示为白色,不透明。还有什么其他方法?您是否尝试通过编程方式设置背景?它似乎会出现Null异常或其他情况,您可以尝试第二种回答如果我在创建对话框后在主活动中编写此项,它将返回Null并导致应用程序崩溃。或者在onStart()中)问题是我把它放在
builder.create()
调用之前,这就是为什么它返回nullIs,将其在布局中设置为不可见,就像在
builder.create();
之后设置为不可见一样,我使用
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
?您可以在XML布局中设置透明颜色。这更简单。如果调用不正确,则从代码中设置颜色可能会导致错误。
    // set the dialog background to transparent
    getDialog().getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

    // remove background dim 
    getDialog().getWindow().setDimAmount(0);