java - 如何使对话框 fragment 背景不可见?

标签 java android android-studio

我在 Flow 的方法中使用了 youtuber Coding 来创建自定义对话框。我整天都在尝试使对话框的背景透明。我已经使用了我在网上找到的每一种方法。没有工作。

事情是这样的:

  • 首先是对话框布局layout_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>
    
  • 这是对话框类:

     public class DialogBrightness extends AppCompatDialogFragment {
    
         //declare whatever variables here
    
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
    
             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
             LayoutInflater inflater = getActivity().getLayoutInflater();
             View view = inflater.inflate(R.layout.layout_dialog, null);
    
             builder.setView(view)
                     .setTitle("Login")
                     .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialogInterface, int i) {
                         }
                     })
                     .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialogInterface, int i) {
                             //get whatever values
                             listener.apply(//values);
                         }
                     });
    
             //findViewById for your dialog contents here
    
             return builder.create();
         }
    
         public interface DialogBrightnessListener {
             void apply(//values);
         }
     }
    
  • 这是从主 Activity 调用的对话框:

    DialogBrightness dialogBrightness = new DialogBrightness();
    dialogBrightness.show(getSupportFragmentManager(), "Brightness Dialog");
    

对话框是这样显示的:

enter image description here

我试图让顶部的白色部分不可见。什么都没用!

最佳答案

试试这个:

将下面的代码放在onCreateDialog中:

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

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

关于java - 如何使对话框 fragment 背景不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63205115/

相关文章:

java - 通用内部类构造函数接受不同类型的参数设置什么?

java - 使用 log4j2 记录 Spring 事务

java - newFixedThreadPool.setCorePoolSize() 不使用线程,创建新的线程,这可能是开销

java - acos 没有给出角度值?

Android 从视频中剥离音频

android - Gradle同步失败-其他各种库都在请求play-services-measurement-base

java - 切换此标记后预期的标签

java - 在 Android 中登录 Facebook 无法正常工作

android - 如何摇动Genymotion创建的虚拟设备(安卓模拟器)

java - 如何修复 "Error: ' ;' expected"或 "Error: ' )' expected"以及 boolean 值中的错误