源码获取方式
- 打开Android Studio,选择 File-New-Import Sample-Navigation Drawer
- 或者直接github:https://github.com/googlesamples/android-NavigationDrawer
主要功能
实现侧边导航栏,平时是隐藏的,通过点击特定按钮或者从屏幕边缘滑动可以打开导航抽屉。
效果图:
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