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下的主题,这样一个看着比较好的侧滑菜单就是实现了


  

   




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值