我有一个BottomSheetDialogFragment,我需要在这个对话框中设置我的高度。我需要,当用户点击一个按钮,对话框将出现,并填补85%的屏幕。该怎么做呢?
发布于 2020-10-13 23:41:47
BottomSheetDialogFragment
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val offsetFromTop = 200
(dialog as? BottomSheetDialog)?.behavior?.apply {
isFitToContents = false
setExpandedOffset(offsetFromTop)
state = BottomSheetBehavior.STATE_EXPANDED
}
}
发布于 2019-11-01 06:24:14
你可以这样做:
public class MyBottomSheetDialog extends BottomSheetDialogFragment {
//....
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override public void onShow(DialogInterface dialogInterface) {
BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;
setupRatio(bottomSheetDialog);
}
});
return dialog;
}
private void setupRatio(BottomSheetDialog bottomSheetDialog) {
//id = com.google.android.material.R.id.design_bottom_sheet for Material Components
//id = android.support.design.R.id.design_bottom_sheet for support librares
FrameLayout bottomSheet = (FrameLayout)
bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();
layoutParams.height = getBottomSheetDialogDefaultHeight();
bottomSheet.setLayoutParams(layoutParams);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
private int getBottomSheetDialogDefaultHeight() {
return getWindowHeight() * 85 / 100;
}
private int getWindowHeight() {
// Calculate window height for fullscreen use
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
}
发布于 2020-09-17 15:48:45
可在BottomSheetDialogFragment()的onCreateDialog中设置始终扩展到全高
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), theme)
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
dialog.behavior.skipCollapsed = true
return dialog
}
可以在onCreateView中设置最大高度
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.bottom_sheet_dialog, container, false)
// Set max height to half screen
view.findViewById<ConstraintLayout>
(R.id.root_layout_of_bottom_sheet).maxHeight =
(resources.displayMetrics.heightPixels * 0.5).toInt()
return view
}
https://stackoverflow.com/questions/58651661
复制相似问题