如何创建具有透明部分的 DialogFragment?

How to create a DialogFragment with a transparent part?

我应该如何创建此设计的高级部分? 我正在使用 DialogFragment 在弹出窗口中执行此操作,但我无法在顶部实现透明效果。

按钮设计但是background_circle:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#fff"/>

</shape>

使用以下 XML 文件 my_dialog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:padding="10dp"
        android:orientation="vertical"
        android:background="#fff"
        android:gravity="center">

        <TextView
            android:layout_marginTop="50dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text long text"
            />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"/>

    </LinearLayout>


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/background_circle"
        android:padding="3dp"
        android:src="@drawable/ic_empty_star"/>
</RelativeLayout>

结果:

星星必须在布局之外的上部。

这可能是粗略的解决方案,但是在弹出视图后面使用模糊视图怎么样?

检查这个库。

https://github.com/mmin18/RealtimeBlurView

这很容易..我的朋友

就像我在 onStart() 中所做的那样,将默认对话框背景设置为透明

class TestDialog: DialogFragment() {

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            return inflater.inflate(R.layout.my_dialog, container)
        }

        override fun onStart() {
            super.onStart()
            // if you want your dialog's width/height match device screen's width/height
            // dialog?.window?.setLayout(
            //    ViewGroup.LayoutParams.MATCH_PARENT,
            //    ViewGroup.LayoutParams.WRAP_CONTENT
            // )

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

你的对话框。xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="30dp"
            android:padding="10dp"
            android:orientation="vertical"
            android:background="#fff"
            android:gravity="center">

                <TextView
                    android:layout_marginTop="50dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="text long text"
                    />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button"/>

            </LinearLayout>


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/background_circle"
            android:padding="3dp"
            android:src="@drawable/ic_account_circle_black_54dp"/>

</RelativeLayout>

background_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#fff"/>

</shape>