1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_height="40dp"
android:layout_width="match_parent"
android:id="@+id/btn"/>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_hideable="true"
app:behavior_peekHeight="100dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<include
layout="@layout/setting"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
2.
View bottomSheet = findViewById(R.id.bottom_sheet);
final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
Button button= (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(behavior.getState()==BottomSheetBehavior.STATE_HIDDEN){
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}else{
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}
});
3.区别:
View bottomSheet = findViewById(R.id.bottom_sheet);
final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
Button button= (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(behavior.getState()==BottomSheetBehavior.STATE_HIDDEN){
/**
* app:behavior_peekHeight="0dp" behavior.setState(BottomSheetBehavior.STATE_EXPANDED); 只能响应一次
*
* app:behavior_peekHeight="0dp" behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 点击时,底部表出不来
*
* app:behavior_peekHeight="100dp" behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 点击按钮时,底部只弹出100dp,
*
* app:behavior_peekHeight="100dp" behavior.setState(BottomSheetBehavior.STATE_EXPANDED); 点击按钮,底部表会完整弹出
*/
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}else{
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}
});