How to use BottomSheetDialog?

前端 未结 5 460
暖寄归人
暖寄归人 2021-02-03 19:41

I want to try BottomSheetDialog introduced in Android Support Library 23.2 but it doesn\'t seem to work correctly. Here is what the doc says:

5条回答
  •  庸人自扰
    2021-02-03 20:33

    Here's the issue on code.google.com https://code.google.com/p/android/issues/detail?id=201793

    An issue some users are seeing boils down to the FrameLayout that wraps our content view being centered vertically. The BottomSheetBehavior only works if this view is top aligned. I haven't figured out what causes the FrameLayout to become centered vertically yet, but here's a possible workaround:

    View contentView = ...
    // You may have to measure your content view first.
    dialog.setContentView(contentView);
    
    // Change this to a percentage or a constant, whatever you want to do.
    // The default is 1024 - any views smaller than this will be pulled off 
    // the bottom of the screen.
    float peekHeight = contentView.getMeasuredHeight();
    
    View parent = (View)contentView.getParent();
    BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
    behavior.setPeekHeight(peekHeight);
    CoordinatorLayout.LayoutParams layoutParams = 
       (CoordinatorLayout.LayoutParams)parent.getLayoutParams();
    layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
    

提交回复
热议问题