如何使 BottomSheetDialog 匹配父级高度(全屏)

ros*_*lin 5 android dialog android-dialog setcontentview bottom-sheet

这是我使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center_horizontal"
android:background="@color/white"
android:gravity="top|center_horizontal"
android:orientation="vertical">

    <TextView
        android:id="@+id/provisioningTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Phone Number" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是我创建对话框的类:

open class DialogProvisioningData : BottomSheetDialog {
constructor(context: Context) : super(context)


private lateinit var mBehavior: BottomSheetBehavior<FrameLayout>

override fun setContentView(view: View) {
    super.setContentView(view)
    val bottomSheet = window.decorView.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout
    mBehavior = BottomSheetBehavior.from(bottomSheet)
    mBehavior.peekHeight = Resources.getSystem().getDisplayMetrics().heightPixels
    mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}

override fun onStart() {
    super.onStart()
    mBehavior.peekHeight = Resources.getSystem().getDisplayMetrics().heightPixels
    mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}

companion object {
    fun newInstance(context: Context): DialogProvisioningData {
        val dialog = DialogProvisioningData(context)
        var layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater;
        val bottomSheet = layoutInflater.inflate(R.layout.dialog_provisioning, null)
        bottomSheet.layout.setOnClickListener({ dialog.cancel() })
        dialog.setOnShowListener { dialog ->
            val d = dialog as BottomSheetDialog

            val bottomSheet = d.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
            BottomSheetBehavior.from(bottomSheet!!).state = BottomSheetBehavior.STATE_EXPANDED
        }
        dialog.setContentView(bottomSheet)
        dialog.show()
        return dialog
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我需要更改什么才能使 BottomSheetDialog 真正全屏显示?我已将状态设置为展开,并将 peekHeight 设置为屏幕高度

ros*_*lin -3

After multiple implementations tried, this was what I was mostly happy. It's full screen, it's a dialog, and it also has the correct SlideUpAnimation that I needed on it. Also, it's easy to call if from inside a fragment without needing to dismiss my own fragment:

open class DialogProvisioningData : Dialog {
constructor(context: FragmentActivity, themeResId: Int) : super(context, themeResId)

    companion object {
        val TAG: String = "DialogProvisioningData"

        fun newInstance(context: FragmentActivity): DialogProvisioningData {
            val dialog = DialogProvisioningData(context, R.style.DialogAnimation)
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
            dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation
            var layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val bottomSheet = layoutInflater.inflate(R.layout.dialog_provisioning, null)

            dialog.setContentView(bottomSheet)
            dialog.show()
            return dialog
        }
    }
}
Run Code Online (Sandbox Code Playgroud)