如 Support Library Features 中所述页; v4 支持库 通过添加 DrawerLayout
添加了对Navigation Drawers 的支持,Design Support Library 添加了对 Navigation 的支持通过添加 NavigationView
(作为对作为依赖项的 DrawerLayout
的补充)的抽屉。
有很多教程如何仅使用 v4 支持库创建抽屉导航,例如Google page on Navigation Drawer .
由于设计支持库添加了对 API 级别 7 的支持,因此推荐和首选的创建抽屉导航的方法是什么。差异在哪里?是否存在设计差异?
what is the recommended and preferred way to create a Navigation
Drawer. Where are the differences? Are there design differences?
实际上,这些并没有什么大的区别。例如,As Google
医生说:
http://developer.android.com/training/implementing-navigation/nav-drawer.html
For example, the following layout uses a DrawerLayout with two child
views: a FrameLayout to contain the main content (populated by a
Fragment at runtime), and a ListView for the navigation drawer.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
但是在Support Library
,您可以使用 NavigationView
轻松实现此目的当然,标准 Material 设计指南是这样的:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!--Contents like Coordinator Layout-->
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer" />
<!--Here is the Drawer menu-->
</android.support.v4.widget.DrawerLayout>
在这种情况下,我们使用了 app:menu="@menu/drawer"
通过一种简单易行的方法来实现这一点,在上面的代码中,他们使用了 <ListView
.
就这些。