在 Kotlin 中调整对话框片段的大小

Resize the size of Dialog Fragment in Kotlin

我尝试了很多方法,但对话框的大小从未改变,我不知道,但有时当我在对话框片段之外单击时,对话框片段不会关闭我的意思是在一些特殊的地方和平仓。

这里是 class:

class PopupWindowFragment : DialogFragment() {
    lateinit var mPopupWindowBinding: FragmentPopupWindowBinding
    lateinit var mPopupViewModel: PopupViewModel
    var id: Long = 0L
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mPopupWindowBinding =
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.fragment_popup_window,
                container,
                false
            )
        return mPopupWindowBinding.root
    }
}

这是导航:

<dialog
        android:id="@+id/navigation_dialog_fragment"
        android:name="com.example.holyquran.ui.popupWindow.PopupWindowFragment"
        tools:layout="@layout/fragment_popup_window">

编辑 这是布局:

<?xml version="1.0" encoding="utf-8"?>
    
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
        <data>
        <variable
            name="viewModel"
            type="com.example.holyquran.ui.popupWindow.PopupViewModel" />

        <variable
            name="userName"
            type="com.example.holyquran.data.model.UserInfo" />

        <variable
            name="loan"
            type="com.example.holyquran.data.model.Loan" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20sp"
        tools:context=".ui.popupWindow.PopupWindowFragment">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@{userName.fullName}"
                android:textColor="@color/black"
                android:textSize="25sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToIncrease()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="@string/submit_increase"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="left"
                android:src="@drawable/ic_baseline_add_circle_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToDecrease()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/submit_decrease"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_add_circle_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToLoan()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/submit_loan"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_add_circle_24" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/payPaymentLL"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToPayPayments()}"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/noLoanForUser"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="@string/no_loan_user"
                android:textColor="@color/gray600"
                android:textSize="14sp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/payPayment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/pay_payment"
                android:textColor="@color/black"
                android:textSize="20sp" />


            <ImageView
                android:id="@+id/img"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_add_circle_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1.5dp"
            android:layout_marginTop="10dp"
            android:background="@color/gray500" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToLoanList()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/payments_list"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_format_list_bulleted_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToUserTransactionHistory()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/transaction_history"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_format_list_bulleted_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1.5dp"
            android:layout_marginTop="10dp"
            android:background="@color/gray500" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToEditUserInfo()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/edit_user_info"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_edit_24" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/deleteUser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.deleteUser()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/delete_user"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_delete2" />
        </LinearLayout>
    </LinearLayout>
</layout>

我试过 this and even this 但 NONE 成功了。

感谢任何帮助:)

您可以通过更改对话框的大小来调整 DialogFragment 的宽度和高度 window;

例如设置 300dp 宽度和 400dp 高度:

dialog!!.window!!.setLayout(
        300.toPx(requireContext()),
        400.toPx(requireContext())

但是在像 onViewCreated()onStart() 中那样创建对话框布局视图之后执行此操作;不在 onCreateView():

class PopupWindowFragment : DialogFragment() {

    //..... your rest of code

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (dialog != null) dialog!!.window!!.setLayout(
            300.toPx(requireContext()),
            400.toPx(requireContext())
        )

    private fun Int.toPx(context: Context): Int =
        (this * context.resources.displayMetrics.density).toInt()

}

I want to make the Dialog fragment smaller than what is it now because there isn't much space between the dialog fragment and the bottom of the phone.

现在你需要知道对话框当前的宽高,这需要监听对话框的viewTreeObserver

将 id 添加到对话框的根 ViewGroup (LinearLayout): say dialog_root

<LinearLayout 
    android:id="@+id/dialog_root"

然后在根布局的 viewTreeObserver` 中更改高度和宽度:

class PopupWindowFragment : DialogFragment() {

    //..... your rest of code


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mPopupWindowBinding.dialogRoot.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                rootLayout.viewTreeObserver
                    .removeOnGlobalLayoutListener(this)

                // save default base layout width & height
                val height = rootLayout.height.toDp(requireContext())
                val width = rootLayout.width.toDp(requireContext())

                // change the width & height of the dialog (reduce by 50 dp)
                if (dialog != null) dialog!!.window!!.setLayout(
                    (width - 50).toPx(requireContext()),
                    (height - 50).toPx(requireContext())
                )
            }
        })

    private fun Int.toPx(context: Context): Int =
        (this * context.resources.displayMetrics.density).toInt()

    fun Int.toDp(context: Context): Int = (this / context.resources.displayMetrics.density).toInt()

}