Android开发:如何将Bottom Sheet高度设置为match_parent?
实现底部对话框高度铺满屏幕(match_parent)
要让Modal Bottom Sheet的高度扩展至全屏(等同于空Activity的高度),可以通过修改BottomSheetDialogFragment的代码来实现,以下是两种可行方案:
方案一:通过BottomSheetBehavior强制全屏
重写BottomSheetDialogFragment的onStart()方法,获取弹窗的Behavior并调整参数,强制弹窗完全展开:
import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import androidx.annotation.Nullable; import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.bottomsheet.BottomSheetDialog; import com.google.android.material.bottomsheet.BottomSheetDialogFragment; public class BottomSheetDialog extends BottomSheetDialogFragment { @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.buttom_sheet_layout, container, false); return v; } @Override public void onStart() { super.onStart(); BottomSheetDialog dialog = (BottomSheetDialog) getDialog(); if (dialog != null) { View bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet); if (bottomSheet != null) { BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet); // 设置弹窗为完全展开状态 behavior.setState(BottomSheetBehavior.STATE_EXPANDED); // 获取屏幕高度,设置弹窗高度为全屏 WindowManager windowManager = getActivity().getWindowManager(); int screenHeight = windowManager.getDefaultDisplay().getHeight(); behavior.setPeekHeight(screenHeight); // 强制布局参数为match_parent bottomSheet.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, screenHeight )); } } } }
方案二:通过对话框监听修改布局参数
在onCreateDialog()方法中监听弹窗显示事件,直接修改底部布局的参数为全屏:
import android.app.Dialog; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.google.android.material.bottomsheet.BottomSheetDialog; import com.google.android.material.bottomsheet.BottomSheetDialogFragment; public class BottomSheetDialog extends BottomSheetDialogFragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.buttom_sheet_layout, container, false); } @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState); dialog.setOnShowListener(dialogInterface -> { BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface; View bottomSheet = bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet); if (bottomSheet != null) { // 强制布局宽高为match_parent bottomSheet.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT )); } }); return dialog; } }
额外注意
确保你的buttom_sheet_layout.xml根布局高度设置为match_parent,避免布局自身限制高度:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <!-- 弹窗内部内容 --> </LinearLayout>
内容的提问来源于stack exchange,提问作者user18148338
相关产品推荐
相关产品推荐

