Difference between navigationdrawer Android Doc an

2019-03-05 06:23发布

I want to implement a navigation drawer, and try to understand how it works. I have tested the navigationDrawerActivity that we can choose in Android Studio with an activity_main as following :

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

And looking at the doc Android : http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html, the activity_main is :

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

So I don't understand why it's different between android studio and the doc. Can you tell me why is there this difference? And the doc uses fragment, whereas the android studio activity doesn't.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-05 07:04

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

The template used in Android Studio is more recent than the doc which is not updated.
With the new 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?

The only difference is the use of the NavigationView instead of a ListView.
Of course you can use what you want inside the DrawerLayout.

查看更多
成全新的幸福
3楼-- · 2019-03-05 07:07

The doc just provides a way to demonstrate how simply DrawerLayout works, so you will learn how to use basic stuff like creating the drawer, handle navigation clicks and open/close the drawer.

The activity_main comes from Android Studio is not only that, it also brings some material design to your app. Stuff like the NavigationView, AppBarLayout they all come from Android Design Support Library. But the usage of DrawerLayout is more or less the same.

查看更多
登录 后发表回答