android 侧滑菜单

在DrawerLayout出现之前,我们需要做侧滑菜单时,不得不
自己实现一个或者使用Github上的开源的项目SlidingMenu

现在我所讲的是google后期添加的同样功能的组件

效果

这里写图片描述

DrawerLayout

一个布局组件,只要按照drawerLayout的规定布局方式写完布
局,就有侧滑的效果。
一般作为根布局,否则可能会出现触摸事件被屏蔽的问题

<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"     android:layout_height="match_parent">

让其他控件成为侧边栏
在子控件中加入属性

    android:layout_gravity

属性值可以为right,left,strat,end

方法; openDrawer(Gracity.LEFT/RIGHT)
closeDraawer(Gracity.LEFT/RIGHT)

Google在5.0之后推出了NavitationView,就是我们测滑出来的那个菜单。菜单分为两部分,上面是HeaderLayout,下面是一个menu。
和DrawerLayout一起使用。
独特属性
app:headerLayout:引用一个头布局文件,包括背景图片上面的显示用户名的控件等
.app:menu:引用一个menu
效果图代码

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerlayout"
    android:background="#e2e4fe">
    ...
    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/left_menu"
        android:layout_gravity="left"
        app:headerLayout="@layout/left_menu_header"
        app:menu="@menu/left_menu">
    </android.support.design.widget.NavigationView>
    ...

layout/left_menu_header

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#d2fb85">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/headima"
            android:background="#faf3f3"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="用户名"
            android:id="@+id/textView2"
            android:layout_below="@+id/headima"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>

menu/left_menu

 <?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="登录"
        android:id="@+id/left_menu_login"
        android:icon="@drawable/note"></item>
    <item android:title="设置"
        android:id="@+id/left_menu_setting"
        android:icon="@android:drawable/ic_menu_manage"></item>
    <item android:title="头像"
        android:id="@+id/left_menu_head"
        android:icon="@android:drawable/ic_menu_gallery"></item>
</menu>

获取头部组件

left_menu=(NavigationView)findViewById(R.id.left_menu) ;
headerview=left_menu.getHeaderView(0);
headima=(ImageView)headerview.findViewById(R.id.headima);

menu点击方法

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {       @Override       
public boolean onNavigationItemSelected(MenuItem item) {
return true;       
}   
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值