简体   繁体   中英

Is there a proper way to use the Appcompat “NavigationView” as permanent (see Material Design guidelines)

By reading the Material Design guidelines about the Navigation Drawer (Drawerlayout + NavigationView) I see there is a recommandation to set it always opened on desktop. That's what I would like to achieve on tablets.

Permanent navigation drawers are always visible and pinned to the left edge, at the same elevation as the content or background. They cannot be closed.

在此输入图像描述

But it seems that the widget offered in support library is not easy to manipulate and achieve such an effect.

Did I missed something, or is there an easy way to let it opened on tablets/desktop/TV?

My current phone layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primarycolor"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>


<!-- The navigation drawer   style="@style/NavDrawer"

                 android:background="@android:color/white"-->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    app:headerLayout="@layout/nav_header"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/nav" />

If you don't want to use navigation view as a drawer do not put the NavigationView directly inside the DrawerLayout .

<DrawerLayout>
    <LinearLayout>
        <NavigationView android:layout_width="320dp" />
        <LinearLayout android:orientation="vertical">
            <Toolbar/>
            <FrameLayout android:id="@+id/content_frame" />
        </LinearLayout>
    </LinearLayout>
</DrawerLayout>

Put this layout inside /res/layout-sw600dp-land . Your original layout will be loaded on phones and this one on 7" tablets and above in landscape.

In addition to DevrimTuncer answer, I have found this post: http://derekrwoods.com/2013/09/creating-a-static-navigation-drawer-in-android/

In addition to:

 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, drawerList);

We must also set:

drawerLayout.setScrimColor(Color.TRANSPARENT);

And add some margins:

values/dimens.xml

<resources>
    <dimen name="drawer_size">240dp</dimen>
    <dimen name="drawer_content_padding">0dp</dimen>
</resources>

values-sw720dp-land/dimens.xml

<resources>
    <dimen name="drawer_content_padding">240dp</dimen>
</resources>

I would create a boolean resource value and use it to set drawer lock mode for this. For instance:

res
  -- values
    -- drawer_layout_values.xml
         <bool name="drawer_layout_locked">false</bool>
  -- values-sw720dp
    -- drawer_layout_values.xml
         <bool name="drawer_layout_locked">true</bool>

Code:

if(getResources().getBoolean(R.bool.drawer_layout_locked)) {
    drawerLayoutInstance.setDrawerLockMode (DrawerLayout.LOCK_MODE_LOCKED_OPEN)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM