【android】NavigationView控件的使用

本文介绍了Android中的NavigationView控件,它是Google在Android5.0后推出的用于创建抽屉菜单的组件,兼容Android2.1。内容包括NavigationView的组成部分——HeaderLayout和menu的设置方法,以及如何编写布局文件和处理点击事件。
摘要由CSDN通过智能技术生成

这是Google在Android5.0之后推出的一个控件,兼容到Android2.1,代替之前自己做抽屉菜单,简单方便。NavigationView整体分为上下两个部分,上部分叫做HeaderLayout,下面的点击项都是menu。

1、写布局文件

整体布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmls:app="http://schemas.android.com/apk/res-auto"
xmls:tools="http://schemas.android.com/tools"
android:id="@_id/item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

    <include
        layout="@layout/weather_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>

代码解释:最外层是个DrawerLayout,

headerLayout

看一下headerLayout(app:headerLayout=”@layout/nav_header_main”)的代码:

<?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="160dp"
    android:background="#81c0c0"
    android:gravity="bottom"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    andro
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值