底页对话框横向宽度问题

问题描述 投票:0回答:2
android dialog material-design width bottom-sheet
2个回答
9
投票

-> 你的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");
});

0
投票

在科特林中:

val dialog = BottomSheetDialog(context).apply {
    setContentView(bottomSheet.root)
    behavior.maxWidth = ViewGroup.LayoutParams.MATCH_PARENT
    behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
© www.soinside.com 2019 - 2024. All rights reserved.