如何更改BottomSheetDialog的默认高度?

ian*_*ake 40 android android-support-library android-support-design

我一直在使用支持库23.2中添加的新BottomSheetDialog ,但我想更改对话框的默认高度.我知道它可能与behavior_peekHeight控制初始高度的属性有关,但是如何BottomSheetDialog在我无法直接访问BottomSheetBehavior?时设置它?

ian*_*ake 69

您可以bottomSheetDialogTheme在Activity中设置a ,覆盖bottomSheetStyle属性behavior_peekHeight:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>

<style name="AppBottomSheetDialogTheme"
       parent="Theme.Design.Light.BottomSheetDialog">
  <item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>

<style name="AppModalStyle"
       parent="Widget.Design.BottomSheet.Modal">
  <item name="behavior_peekHeight">@dimen/custom_peek_height</item>
</style>
Run Code Online (Sandbox Code Playgroud)

可以使用此相同的技术对其它属性,以及,如添加<item name="behavior_hideable">true</item>AppModalStyle改变底部片材是否是可隐藏.

  • @JaredRummler - [明确鼓励](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/)回答你自己的问题,尽管我也更喜欢官方文件. (7认同)
  • 由于您与Chris Banes和支持团队密切合作,因此发布此处有点愚蠢.这不是一个"真实"的问题; 你已经得到了答案并在同一分钟发布了它.对于支持库恕我直言,应该有更好的文档和指南.我很欣赏你的辛勤工作. (5认同)
  • @AmJay - 应该在版本23.2.1中修复.一定要更新! (4认同)
  • 使用Theme.AppCompat.Light.NoActionBar作为父布局时,这不起作用 (3认同)
  • @ianhanniballake我很抱歉。很高兴知道。 (2认同)

小智 23

你可以BottomSheetBehavior在你的代码中使用

BottomSheetDialog dialog = new BottomSheetDialog(content);
.
.
.
dialog.setContentView(view);
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) view.getParent());
mBehavior.setPeekHeight(your dialog height)
dialog.show();
Run Code Online (Sandbox Code Playgroud)

  • getParent() 给出 null (2认同)

Hel*_*Csl 14

为我炒菜

  override fun onStart() {
    super.onStart()
    dialog?.also {
        val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
        bottomSheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
        val behavior = BottomSheetBehavior.from<View>(bottomSheet)
        behavior.peekHeight = resources.displayMetrics.heightPixels //replace to whatever you want
        view?.requestLayout()
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 它对我有用,只是更改为 WRAP_CONTENT BottomSheet.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT (2认同)

Fan*_*ang 13

styles.xml

<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>

<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
    <item name="behavior_peekHeight">500dp</item>
</style>

BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
            dialog.setContentView(R.layout.layout_bottom_sheet);
            dialog.show();
Run Code Online (Sandbox Code Playgroud)


iva*_*8m8 11

老实说,我不知道为什么没有人提到这些简单的方法,但是:

override fun onResume() {
    super.onResume()

    dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
    // or since com.google.android.material:material:1.1.0-beta01
    (dialog as? BottomSheetDialog)?.behavior?.state = BottomSheetBehavior.STATE_EXPANDED

}
//or
dialog.behavior.peekheight = YOUR_VALUE
Run Code Online (Sandbox Code Playgroud)

直接回答问题

:如何直接访问BottomSheetBehavior 的高度?

dialog.behavior.peekheight

  • BottomSheetDialog.behavior 现在是私有的 (4认同)
  • @Aguragorn,我可以看到它是[已定义](https://github.com/material-components/material-components-android/blob/708c50f8d23f195e76663daf274c6b935ddc8aef/lib/java/com/google/android/material/bottomsheet/BottomSheetDialog.java #L226) `公共` (2认同)

ked*_*ked 6

我的底部工作表对话框具有回收器视图,并且没有可显示的项目,因此它正在缩小,但假设您想将窥视高度设置为 85%,无论 Recyclerview 中是否存在项目,请尝试此解决方案。

在扩展 BottomSheetDialogFragment 的类中添加以下代码

override fun onStart() {
    super.onStart()
    (dialog as BottomSheetDialog).behavior.peekHeight = getBottomSheetDialogDefaultHeight()
    dialog?.let {
        val bottomSheet = it.findViewById<View>(R.id.design_bottom_sheet)
        bottomSheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
    }
}

private fun getBottomSheetDialogDefaultHeight(): Int {
    val displayMetrics = DisplayMetrics()
    (context as Activity?)?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
    return displayMetrics.heightPixels * 85 / 100
}
Run Code Online (Sandbox Code Playgroud)


Nik*_*ski 5

另一种方法是继承BottomSheetDialogFragment并控制设置内容视图的方式和时间。在视图树上,您可以得到BottomSheetDialog包装内容视图的行为。这不是一个很好的解决方案,因为它需要更多的布局遍历。重要的是,当底部状态为“状态”时,STATE_HIDDEN必须关闭对话框,否则,我们显然会违反库中提供的实现。

通过编程设置窥视高度后,必须调用内容视图requestLayout(),这实际上是另一次布局传递。

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {


    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            setStateText(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.bottom_sheet_dialog_content_view, null);
        dialog.setContentView(contentView);
        mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
        if (mBottomSheetBehavior != null) {
           mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);    
           mBottomSheetBehavior.setPeekHeight(peekHeight);
           contentView.requestLayout();
        }
}
Run Code Online (Sandbox Code Playgroud)