谷歌设计支持库中有一个名为 BottomSheet 的可用行为,您可以使用它来实现该行为。将此添加到您的 App 模块 gradle 中
compile 'com.android.support:design:23.2.0'
然后在你的xml中添加底部
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:clipToPadding="true"
android:background="@android:color/holo_orange_light"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/ipsum"
android:padding="16dp"
android:textSize="16sp"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
注意 NestedScrollView 是给定的 BottomSheetBehavior
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
然后是Java文件
private BottomSheetBehavior mBottomSheetBehavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bottomSheet = findViewById( R.id.bottom_sheet );
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
}
然后显示底部工作表使用
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
并隐藏底部工作表使用
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
欲了解更多信息,请查看
BottomSheet Example