navigationdrawer Android Doc 和 navigationDrawerActivity 的区别

Difference between navigationdrawer Android Doc and navigationDrawerActivity

我想实现一个导航抽屉,并尝试了解它是如何工作的。我已经测试了我们可以在 Android Studio 中使用 activity_main 选择的 navigationDrawerActivity,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        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>

并查看文档 Android:http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html,activity_main 是:

<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>

所以我不明白为什么 android studio 和 doc 不一样。你能告诉我为什么会有这种差异吗?文档使用片段,而 android 工作室 activity 没有。

该文档只是提供了一种方法来演示 DrawerLayout 的工作原理,因此您将学习如何使用基本内容,例如创建抽屉、处理导航点击和 open/close 抽屉。

activity_main 来自 Android Studio 不仅如此,它还为您的应用程序带来了一些 material 设计。 NavigationViewAppBarLayout之类的东西都来自Android Design Support Library。但是DrawerLayout的用法大致相同。

So I don't understand why it's different between android studio and the doc.

Android Studio 中使用的模板比未更新的文档更新。
随着新 Design Support Library you can use the NavigationView inside your DrawerLayout to achieve in a very simple way a NavigationDrawer which follows the material guidelines.

Can you tell me why is there this difference?

唯一的区别是使用 NavigationView 而不是 ListView。
当然你可以在DrawerLayout里面使用你想要的东西。