-> 你的xml布局没有问题,可以用代码解决。我在下面提供了一个解决此问题的示例。
=> 底表类代码
public class BottomSheet extends BottomSheetDialogFragment {
private View view;
private BottomSheetBehavior mBottomBehavior;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
//Do your logic code here
return view;
}
@Override
public void onStart() {
super.onStart();
mBottomBehavior = BottomSheetBehavior.from((View) view.getParent());
mBottomBehavior.setMaxWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mBottomBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
=> 活动类别代码
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
//To open the bottom sheet, NB. make sure 'BottomSheet' is the name you declared Bottom sheet class
BottomSheet bottomSheet = new BottomSheet();
bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");
});
在科特林中:
val dialog = BottomSheetDialog(context).apply {
setContentView(bottomSheet.root)
behavior.maxWidth = ViewGroup.LayoutParams.MATCH_PARENT
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}