如何使BottomSheetDialog高度充满父容器(全屏显示)

5

这是我使用的布局:

<?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>

这是我创建对话框的类:

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
    }
}
}

我需要将BottomSheetDialog调整为全屏,我已经将状态设置为expanded,并将peekHeight设置为屏幕高度。

这个回答解决了你的问题吗?将BottomSheetDialogFragment的高度设置为全屏 - Ryan M
3个回答

0
我们可以通过设置 view.minHeight 来解决这个问题:
class SortProductBottomSheet : BaseBottomSheet() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = inflater.inflate(R.layout.dialog_blablabla, container, false)-

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

        //THIS IS OUR LIFESAVER
        val dm = Resources.getSystem().displayMetrics
        val rect = dm.run { Rect(0, 0, heightPixels, widthPixels) }
        view.minimumHeight = rect.height()
    }

} 

-1
不要使用全屏底部表格。 你可以使用DialogFragment来显示对话框:
用于显示对话框:
  FragmentManager fragmentManager = getSupportFragmentManager();
    DialogFullscreenFragment newFragment = new DialogFullscreenFragment();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.add(android.R.id.content, newFragment).addToBackStack(null).commit();

DialogFullscreenFragment 类:

public class DialogFullscreenFragment extends DialogFragment {


private View root_view;
private Slider slider;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    root_view = inflater.inflate(R.layout.dialog_event, container, false);


    return root_view;
}


@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}

}

1
我不想使用Fragment,我可以使用CustomDialog代替BottomSheetView。但是我不想在我的Activity中再添加另一个Fragment。因此,我更倾向于不使用DialogFragment。 - rosu alin
如果原始问题是关于BottomSheetDialog的,为什么要建议更改整个逻辑呢? - Alberto M

-3
尝试了多个实现后,我最满意的就是这个。它是全屏的,是一个对话框,并且还具备我需要的正确的 SlideUpAnimation。此外,在片段内调用它也很容易,而不需要关闭我的片段:
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
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接