Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單
本文實(shí)例為大家分享了使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單的具體代碼,供大家參考,具體內(nèi)容如下
第一步:添加依賴:
compile "com.android.support:design:${project.properties.get("support")}"
第二步:布局引用:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#04827c">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//頁(yè)面布局
</RelativeLayout>
<include //引入菜單布局
android:id="@+id/au_bottom_sheet"
layout="@layout/view_audio_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior" />
</android.support.design.widget.CoordinatorLayout>
第三步:使用:
//找到BottomSheetBehavior BottomSheetBehavior behavior = BottomSheetBehavior.from(findViewById(R.id.au_bottom_sheet)); //設(shè)置彈出高度 behavior.setPeekHeight(WindowHeight / 2); //默認(rèn)隱藏 behavior.setState(BottomSheetBehavior.STATE_HIDDEN); //添加消失監(jiān)聽(tīng) behavior.setBottomSheetCallback(bottomSheetCallback);
添加菜單消失監(jiān)聽(tīng):
//狀態(tài)監(jiān)聽(tīng),通過(guò)這個(gè)監(jiān)聽(tīng)菜單是否消失
private BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
if (!isBehaviorShowing(behavior)) {
//菜單已經(jīng)消失
}
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//滑動(dòng)監(jiān)聽(tīng)
}
};
判斷是否可見(jiàn):
private Boolean isBehaviorShowing(BottomSheetBehavior behavior) {
return behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED
|| behavior.getState() == BottomSheetBehavior.STATE_EXPANDED
|| behavior.getState() == BottomSheetBehavior.STATE_SETTLING;
}
收回菜單:
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
彈出菜單:
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android底部菜單欄實(shí)現(xiàn)的實(shí)例代碼
- android SectorMenuView底部導(dǎo)航扇形菜單的實(shí)現(xiàn)代碼
- android實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄的示例
- Android仿微信底部菜單欄效果
- Android仿網(wǎng)易嚴(yán)選底部彈出菜單效果
- Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法
- Android自定義控件實(shí)現(xiàn)底部菜單(下)
- Android自定義控件實(shí)現(xiàn)底部菜單(上)
- Android如何實(shí)現(xiàn)底部菜單固定到底部
相關(guān)文章
Flutter應(yīng)用框架搭建實(shí)現(xiàn)屏幕適配方案詳解
移動(dòng)設(shè)備多樣性,特別是Android的碎片化嚴(yán)重,存在各種各樣的分辨率,flutter跨平臺(tái)開(kāi)發(fā)又需要同時(shí)支持Android和IOS,為盡可能的還原設(shè)計(jì)圖效果提升用戶的體驗(yàn),根據(jù)設(shè)計(jì)稿設(shè)計(jì)屏幕ui的時(shí)候我們需要考慮到屏幕適配的問(wèn)題2022-11-11
Android省市區(qū)三級(jí)聯(lián)動(dòng)控件使用方法實(shí)例講解
最近有需求需要實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng),但是發(fā)現(xiàn)之前的實(shí)現(xiàn)不夠靈活,自己做了一些優(yōu)化。下面通過(guò)實(shí)例代碼給大家介紹下Android省市區(qū)三級(jí)聯(lián)動(dòng)控件使用方法2017-01-01
Android實(shí)現(xiàn)底部半透明彈出框PopUpWindow效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部半透明彈出框PopUpWindow效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android開(kāi)發(fā)之圖片切割工具類定義與用法示例
這篇文章主要介紹了Android開(kāi)發(fā)之圖片切割工具類定義與用法,結(jié)合實(shí)例形式分析了Android圖片切割工具類的定義與簡(jiǎn)單使用方法,需要的朋友可以參考下2017-11-11
Android開(kāi)發(fā)之Fragment懶加載的幾種方式及性能對(duì)比
這篇文章主要介紹了Android開(kāi)發(fā)之Fragment懶加載的幾種方式及性能對(duì)比的相關(guān)資料,具體詳細(xì)介紹需要的小伙伴可以參考下面文章內(nèi)容2022-05-05
Android開(kāi)發(fā)之獲取短信驗(yàn)證碼后按鈕背景變化并且出現(xiàn)倒計(jì)時(shí)
在開(kāi)發(fā)是經(jīng)常會(huì)遇到獲取短信驗(yàn)證碼,然后獲取驗(yàn)證碼后需要等待n秒倒計(jì)時(shí),這時(shí)是不能再次發(fā)送短信請(qǐng)求的,這是需要一個(gè)倒計(jì)時(shí)程序,本文給大家分享了實(shí)現(xiàn)此功能的代碼,需要的朋友參考下2016-01-01
Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能
這篇文章主要介紹了Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
Android中ListView的幾種常見(jiàn)的優(yōu)化方法總結(jié)
Android中的ListView應(yīng)該算是布局中幾種最常用的組件之一,本篇文章主要做了三種優(yōu)化總結(jié),有興趣的可以了解一下。2017-02-02
Android中如何實(shí)現(xiàn)清空搜索框的文字
本文主要介紹Android中實(shí)現(xiàn)清空搜索框的文字的方法。項(xiàng)目中的有關(guān)搜索的地方,加上清空文字的功能,目的是為了增加用戶體驗(yàn),使用戶刪除文本更加快捷。需要的朋友一起來(lái)看下吧2016-12-12

