在BottomSheetDialogFragment中更改PeekHeight

时间:2017-01-29 16:48:08

标签: android material-design android-support-design

我正在使用BottomSheetDialogFragment.E Everything是完美的,但我有一个问题。我无法在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());
        }
    });

可以在片段中更改PeekHeight吗?如果有人知道解决方案,请帮助我 感谢

2 个答案:

答案 0 :(得分:0)

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()更改为您想要的任何内容。

答案 1 :(得分:0)

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

    @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);
            }
        });
    }

,您的底层布局必须是这样的:coordinatorlayout

<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>