我尝试在ButtonSheetDialogFragment
布局中设置Margin,但它不起作用。我已经尝试从布局和编程方式设置Margin,但结果相同。
这是我的XML文件layout_bts_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#00000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</FrameLayout>
这是我的Java代码
public class ButtomSheetFragment extends BottomSheetDialogFragment {
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.layout_bts_item, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
int height = LayoutUtils.getScreenHeight(getActivity());
double desiredHeight = (0.85 * height);
((BottomSheetBehavior) behavior).setPeekHeight((int) desiredHeight);
contentView.getLayoutParams().height = (int) desiredHeight;
((FrameLayout.LayoutParams) contentView.getLayoutParams()).leftMargin = 100;
}
}
}
ButtonSheetDialogFragment
是什么? - Code-Apprentice