依賴于CoordinatorLayout和BottomSheetBehavior,需要將底部菜單布局作為CoordinatorLayout的子View洪囤,實現(xiàn)簡單但不夠靈活,適用于底部菜單布局穩(wěn)定的情況大溜。
布局文件
<!--父布局必須是 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"/>
<!--當(dāng)作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"http://控制bottomSheet 初始顯示的高度
app:layout_behavior="@string/bottom_sheet_behavior"
android:background="@color/colorPrimary">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="人生若只如初見泳炉,何事秋風(fēng)悲畫扇。"
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="何如薄幸錦衣郎妆偏,比翼連枝當(dāng)日愿。"
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 的 狀態(tài)改變 根據(jù)不同的狀態(tài) 做不同的事情*/
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://折疊 是顯示剛開始預(yù)設(shè)的高度 也就是app:behavior_peekHeight這個實行的值
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
break;
case R.id.btn_hide://隱藏 就是完全隱藏
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
break;
}
}
}