Difference between navigationdrawer Android Doc and navigationDrawerActivity

拥有回忆 提交于 2019-12-20 06:40:35

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/34799764/difference-between-navigationdrawer-android-doc-and-navigationdraweractivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!