【问题标题】:Change PeekHeight in BottomSheetDialogFragment在 BottomSheetDialogFragment 中更改 PeekHeight
【发布时间】:2017-06-14 20:53:56
【问题描述】:

我正在使用 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());
        }
    });

是否可以在我的 Fragment 中更改 PeekHeight?如果有人知道解决方案,请帮助我 谢谢

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-25
        • 1970-01-01
        • 1970-01-01
        • 2017-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-28
        相关资源
        最近更新 更多