Android 持久底部 sheet 初始可见性

Android persistent bottom sheet initial visibility

我正在尝试在我的布局中实现一个持久底部 sheet - 一个不能完全隐藏但总是从底部窥视并且可以扩展到全高的底部。这是我的布局:

<android.support.design.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="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">

        <!-- main content -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom"
        android:background="@drawable/custom_background"
        android:clipToPadding="false"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        app:behavior_hideable="false"
        app:behavior_peekHeight="50dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <!-- bottom sheet content -->

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

我希望发生的事情是底部 sheet 在我降落在屏幕上后立即可见并折叠,但事实并非如此 - 它被隐藏了。我可以通过在 onCreate() 中调用 bottomSheetBehaviour.setState(BottomSheetBehavior.STATE_EXPANDED) 来让它显示出来。奇怪的是,这只会稍微扩展它 - 超过指定的 peek 高度但小于它应该占据的完整高度。在它出现在这种状态后,我可以将它上下拖动到它应该在的位置并且它工作正常。问题是屏幕上的初始着陆搞砸了。

我确定有一种方法可以使它正常工作,所以底部 Sheet 最初出现在它的窥视高度。有什么想法吗?

正如经常发生的那样 - 提出问题后您会找到答案。我的问题是 android:layout_gravity="center|bottom" 设置在底部 sheet 视图中。删除它修复它。