依赖于CoordinatorLayout和BottomSheetBehavior,需要将底部菜单布局作为CoordinatorLayout的子View,实现简单但不够灵活,适用于底部菜单布局稳定的情况。
布局文件
<!--父布局必须是 CoordinatorLayout-->
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<!--页面的主布局-->
<include layout="@layout/content_main"/>
<!--当作bottomSheet的布局-->
<include layout="@layout/content_bottom_sheet" />
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_expand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Expand_BottomSheet"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_collapsed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Collapsed_BottomSheet"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hide_BottomSheet"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_bottomsheet_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BottomSheetDialog"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_bottomsheet_dialog_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BottomSheetDialogFragment"/>
</LinearLayout>
content_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_content_bottom_sheet"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="true"
app:behavior_peekHeight="50dp"//控制bottomSheet 初始显示的高度
app:layout_behavior="@string/bottom_sheet_behavior"
android:background="@color/colorPrimary">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="人生若只如初见,何事秋风悲画扇。"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="等闲变却故人心,却道故人心易变。"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="骊山语罢清宵半,泪雨霖铃终不怨。"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="何如薄幸锦衣郎,比翼连枝当日愿。"
android:textSize="20sp"
android:gravity="center"/>
</LinearLayout>
activity中使用
public class BottomSheetActivity extends AppCompatActivity implements View.OnClickListener {
private AppCompatButton btnExpand;
private AppCompatButton btnCollapsed;
private AppCompatButton btnHide;
private AppCompatButton btnBottomsheetDialog;
private AppCompatButton btnBottomsheetDialogFragment;
private LinearLayout llContentBottomSheet;
private BottomSheetBehavior bottomSheetBehavior;
private BottomSheetDialog dialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_sheet);
initViews();
}
private void initViews() {
btnExpand = (AppCompatButton) findViewById(R.id.btn_expand);
btnCollapsed = (AppCompatButton) findViewById(R.id.btn_collapsed);
btnHide = (AppCompatButton) findViewById(R.id.btn_hide);
btnBottomsheetDialog = (AppCompatButton) findViewById(R.id.btn_bottomsheet_dialog);
btnBottomsheetDialogFragment = (AppCompatButton) findViewById(R.id.btn_bottomsheet_dialog_fragment);
llContentBottomSheet = (LinearLayout) findViewById(R.id.ll_content_bottom_sheet);
btnExpand.setOnClickListener(this);
btnCollapsed.setOnClickListener(this);
btnHide.setOnClickListener(this);
btnBottomsheetDialogFragment.setOnClickListener(this);
btnBottomsheetDialog.setOnClickListener(this);
/*获取behavior 控制bottomsheet的 显示 与隐藏*/
bottomSheetBehavior = BottomSheetBehavior.from(llContentBottomSheet);
/*bottomSheet 的 状态改变 根据不同的状态 做不同的事情*/
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_COLLAPSED:
Log.e("ccccccccc", "STATE_COLLAPSED");
break;
case BottomSheetBehavior.STATE_DRAGGING:
Log.e("ccccccccc", "STATE_DRAGGING");
break;
case BottomSheetBehavior.STATE_EXPANDED:
Log.e("ccccccccc", "STATE_EXPANDED");
break;
case BottomSheetBehavior.STATE_HIDDEN:
Log.e("ccccccccc", "STATE_HIDDEN");
break;
case BottomSheetBehavior.STATE_SETTLING:
Log.e("ccccccccc", "STATE_SETTLING");
break;
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
/*slideOffset bottomSheet 的 移动距离*/
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_expand://展开
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
break;
case R.id.btn_collapsed://折叠 是显示刚开始预设的高度 也就是app:behavior_peekHeight这个实行的值
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
break;
case R.id.btn_hide://隐藏 就是完全隐藏
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
break;
}
}
}