Google官方Demo NavigationDrawer 侧边导航源码分析

本文分析了Google官方Demo中的Navigation Drawer,详细介绍了如何通过Android Studio获取源码,以及源码的主要功能。内容包括:设置UI布局,如DrawerLayout与RecyclerView的使用,以及如何通过按钮和侧滑操作打开或关闭导航抽屉。此外,还讲解了如何监听导航栏状态,以更新顶部工具栏的标题和按钮。文章最后提到了对原始Demo的扩展,包括添加右侧抽屉和更新ToolBar控制。
摘要由CSDN通过智能技术生成

源码获取方式

主要功能

实现侧边导航栏,平时是隐藏的,通过点击特定按钮或者从屏幕边缘滑动可以打开导航抽屉。

效果图:
这里写图片描述

UI布局

根视图是一个DrawerLayout。
子视图第一个必须是界面的主内容(即导航隐藏时显示的内容),这是因为导航栏在打开时是要遮盖主内容的,所以主内容必须放在导航栏的前面。Demo中是一个FragmentLayout,高宽都设为match_parent,已达到填充整个页面的效果。
其后可以包含一到两个视图,对应左右两侧的导航栏。Demo中只有一个左侧导航栏视图,用的是RecyclerView。Google 建议导航栏的宽度不超过320dp,已保证导航打开时主内容还是部分可见的,Demo中设置为240dp 。这里必须指定android:layout_gravity属性,设置为start代码从左向右滑出,end则代表从右向左滑出。

Activity代码如下:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start" >

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值