Android實(shí)現(xiàn)側(cè)滑菜單DrawerLayout
本文實(shí)例為大家分享了Android實(shí)現(xiàn)側(cè)滑菜單的具體代碼,供大家參考,具體內(nèi)容如下
點(diǎn)擊左側(cè)滑動(dòng)
效果如下
代碼實(shí)現(xiàn)過程:
1.導(dǎo)入框架build.gradle中
//materialDesign implementation 'com.google.android.material:material:1.0.0'
2.xml文件
主要的界面放在DrawerLayout 中,需要強(qiáng)調(diào)的是側(cè)滑菜單也就是下圖顯示的TextView一定要設(shè)置layout_gravity屬性,我是從左側(cè)滑動(dòng)的,所以設(shè)置為start
<androidx.drawerlayout.widget.DrawerLayout 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" android:id="@+id/drawer" android:fitsSystemWindows="true" tools:context=".MainActivity"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" app:layout_constraintTop_toTopOf="parent" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </androidx.constraintlayout.widget.ConstraintLayout> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="#FFFFFF" android:fitsSystemWindows="true" android:text="i am a textView" /> </androidx.drawerlayout.widget.DrawerLayout>
3.MainActivity
綁定xml文件中的toobar
protected void setupToobar() { toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); if (null != getSupportActionBar()) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } }
MainActivity 中將點(diǎn)擊之后觸發(fā)側(cè)邊滑動(dòng)的圖片ic_menu動(dòng)態(tài)放到toolbr中
@Override protected void setupViews() { setupToobar(); drawerLayout = findViewById(R.id.drawer); if (null != getSupportActionBar()) { getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); } }
android.R.id.home 觸發(fā)左側(cè)滑動(dòng)
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: drawerLayout.openDrawer(GravityCompat.START); break; case R.id.item_search: Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show(); } return true; }
到這就結(jié)束了。
4.后話
可以在主內(nèi)容區(qū)里面再放一個(gè)布局,里面放各個(gè)fragment,就可以實(shí)現(xiàn)每個(gè)頁面都有側(cè)滑菜單的效果。
側(cè)滑菜單里面的布局可以新建一個(gè)xml文件,然后include,可以看起來舒服點(diǎn)吧。
其他的效果后面慢慢來吧。
github下載地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)支付寶螞蟻森林水滴浮動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)支付寶螞蟻森林水滴浮動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android開發(fā)RecyclerView單獨(dú)刷新使用技巧
本篇文章主要是分享下RecyclerView中子item如何單獨(dú)刷新以及子item的某一部分內(nèi)容如何實(shí)現(xiàn)單獨(dú)刷新,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09android圖像繪制(四)自定義一個(gè)SurfaceView控件
自定義控件(類似按鈕等)的使用,自定義一個(gè)SurfaceView。如某一塊的動(dòng)態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下2013-01-01Android實(shí)現(xiàn)自動(dòng)提取短信驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)提取短信驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01詳解android特性之CoordinatorLayout用法探析實(shí)例
本篇文章主要介紹了android特性之CoordinatorLayout用法探析實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02Android實(shí)現(xiàn)文件壓縮與解壓工具類
這篇文章主要為大家詳細(xì)介紹了如何使用Android實(shí)現(xiàn)一個(gè)文件壓縮與解壓工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04Android開發(fā)Compose集成高德地圖實(shí)例
這篇文章主要為大家介紹了Android開發(fā)Compose里使用高德地圖實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android開發(fā)實(shí)現(xiàn)簡單的觀察者與被觀察者示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)簡單的觀察者與被觀察者,簡單描述了觀察者模式的概念、原理并結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)觀察者模式的簡單操作技巧,需要的朋友可以參考下2017-11-11Android中區(qū)別Drawable Bitmap Canvas Paint
本文主要介紹Android中Drawable Bitmap Canvas Paint 之間的區(qū)別,這里對這幾個(gè)概念做出詳細(xì)介紹,開發(fā)Android游戲的朋友可以參考下2016-07-07