Android 實現側滑菜單

廢話少說,先上效果

實現側滑的方式有很多,比如slidingmenu,還可以自己自定義控件,但是我這裏用的是系統的NavigationView,既然系統有,我們肯定優先使用系統的

一、實現過程

1、和NavigationView和DrawerLayout搭配使用,實現側滑,就是這麼簡單

二、代碼

     main.xml

     

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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="@android:color/white"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是內容" />
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/headerlayout_head"
        app:menu="@menu/navigation_menu" />

</android.support.v4.widget.DrawerLayout>
NavigationView屬性注意:

    app:menu:是顯示系統的menu文件夾下的文件,NavigationView只能用menu文件

     app:headerLayout:是上部分設置以上帶大icon的佈局

    app:itemBackground:側邊menu條目的背景

    android:background:側邊菜單的背景

    android:layout_gravity="start"(佈局寫好之後一定要加,不然側邊菜單不會隱藏)

navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/aciton_gallery1"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="100"
        android:title="設置1" />
    <item
        android:id="@+id/aciton_gallery2"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="100"
        android:title="設置2" />
    <item
        android:id="@+id/aciton_gallery3"
        android:orderInCategory="100"
        android:title="設置3" />
    <item
        android:id="@+id/aciton_gallery4"
        android:orderInCategory="100"
        android:title="設置4" />
    <item
        android:id="@+id/aciton_gallery5"
        android:orderInCategory="100"
        android:title="設置5">
        <!--子菜單-->
        <menu>
            <item
                android:id="@+id/aciton_gallery51"
                android:icon="@mipmap/ic_launcher"
                android:title="設置5" />
            <item
                android:id="@+id/aciton_gallery52"
                android:icon="@mipmap/ic_launcher"
                android:title="設置5" />
        </menu>

    </item>

</menu>

headerlayout_head.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈" />

</LinearLayout>


注意:

    style要使用Theme.AppCompat下的主題,這樣一個看着比較好的側滑菜單就是實現了


  

   




發佈了48 篇原創文章 · 獲贊 9 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章