CoordinatorLayout
成为根视图。 CoordinatorLayout
是必需的。您的片段或活动视图
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/bottom_sheet_log_out" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
底页
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottomSheetLogOutCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:cardCornerRadius="8dp"
app:cardElevation="@dimen/bottom_sheet_elevation"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
tools:layout_gravity="bottom">
</com.google.android.material.card.MaterialCardView>
此行很重要。app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
MaterialCardView
是可选的,您可以使用任何想要的。代码
private lateinit var bottomSheetLogOutBehavior: BottomSheetBehavior<CardView>
...
// init your bottomsheet
bottomSheetLogOutBehavior = BottomSheetBehavior.from(bottomSheetLogOutCard)
// To Expand
bottomSheetLogOutBehavior.state = BottomSheetBehavior.STATE_EXPANDED
// To hide
bottomSheetLogOutBehavior.state = BottomSheetBehavior.STATE_HIDDEN