Android實現(xiàn)側(cè)滑菜單DrawerLayout
本文實例為大家分享了Android實現(xiàn)側(cè)滑菜單的具體代碼,供大家參考,具體內(nèi)容如下
點擊左側(cè)滑動
效果如下
代碼實現(xiàn)過程:
1.導(dǎo)入框架build.gradle中
//materialDesign implementation 'com.google.android.material:material:1.0.0'
2.xml文件
主要的界面放在DrawerLayout 中,需要強調(diào)的是側(cè)滑菜單也就是下圖顯示的TextView一定要設(shè)置layout_gravity屬性,我是從左側(cè)滑動的,所以設(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 中將點擊之后觸發(fā)側(cè)邊滑動的圖片ic_menu動態(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è)滑動
@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ū)里面再放一個布局,里面放各個fragment,就可以實現(xiàn)每個頁面都有側(cè)滑菜單的效果。
側(cè)滑菜單里面的布局可以新建一個xml文件,然后include,可以看起來舒服點吧。
其他的效果后面慢慢來吧。
github下載地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)RecyclerView單獨刷新使用技巧
本篇文章主要是分享下RecyclerView中子item如何單獨刷新以及子item的某一部分內(nèi)容如何實現(xiàn)單獨刷新,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09android圖像繪制(四)自定義一個SurfaceView控件
自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。如某一塊的動態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下2013-01-01詳解android特性之CoordinatorLayout用法探析實例
本篇文章主要介紹了android特性之CoordinatorLayout用法探析實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02Android開發(fā)實現(xiàn)簡單的觀察者與被觀察者示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)簡單的觀察者與被觀察者,簡單描述了觀察者模式的概念、原理并結(jié)合實例形式分析了Android實現(xiàn)觀察者模式的簡單操作技巧,需要的朋友可以參考下2017-11-11Android中區(qū)別Drawable Bitmap Canvas Paint
本文主要介紹Android中Drawable Bitmap Canvas Paint 之間的區(qū)別,這里對這幾個概念做出詳細介紹,開發(fā)Android游戲的朋友可以參考下2016-07-07