更改底部工作表对话框片段中的窥视高度



我正在工作BottomSheetDialogFragment.一切都很完美,但我有一个问题。我无法更改 BottomSheetDialogFragment 中的 PeekHeight。这是我的来源

public class BottomSheet3DialogFragment extends BottomSheetDialogFragment {
private BottomSheetBehavior mBottomSheetBehavior2;
private BottomSheetBehavior.BottomSheetCallback
        mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            dismiss();
        }
        mBottomSheetBehavior2= BottomSheetBehavior.from(bottomSheet);
        if(mBottomSheetBehavior2!=null)
            mBottomSheetBehavior2.setPeekHeight(20);

    }
    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
    }
};
@Override
public void setupDialog(final Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.fragment_bottomsheet3, null);
    dialog.setContentView(contentView);
}

}

mButton3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BottomSheet3DialogFragment bottomSheetDialogFragment = new BottomSheet3DialogFragment();
            bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
        }
    });

是否可以更改我的片段中的窥视高度?如果有人知道解决方案,请帮助我谢谢

我遇到了同样的问题,这对我有用!

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
            bottomSheet.getLayoutParams().height = 200;
        }
        final View view = getView();
        view.post(new Runnable() {
            @Override
            public void run() {
                View parent = (View) view.getParent();
                CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
                CoordinatorLayout.Behavior behavior = params.getBehavior();
                BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
                bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
                ((View)bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT);
            }
        });
    }

你的底表布局必须是这样的:协调员布局

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:background="@color/gray">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
        <ImageView
            android:id="@+id/img_edit"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginRight="30dp"
            app:srcCompat="@drawable/icon_edit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
        <ImageView
            android:id="@+id/img_delete"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginLeft="25dp"
            app:srcCompat="@drawable/delete_icon"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

dialog.setContentView(contentView);之后你必须把这个:

dialog.setOnShowListener(new OnShowListener() {
  @Override
  public void onShow(DialogInterface dialog) {
    FrameLayout bottomSheet = dialog.getWindow()
        .findViewById(android.support.design.R.id.design_bottom_sheet);
    CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
    coordinatorLayout.getParent().requestLayout();
  }
});

此代码会自动调整高度以查看高度。如果您想要固定高度,只需将bottomSheet.getHeight()更改为您想要的任何高度。

最新更新