How to position menu items in Navigation View?

空扰寡人 提交于 2019-12-23 08:47:26

问题


My question is related to recently released Navigation View from Design Support Library.

I want to position Log In at the bottom of the Navigation View, how I can do this? My menu XML right now, look like this :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_sub_item_1"
            android:icon="@drawable/ic_action_event"
            android:title="@string/home" />
        <item
            android:id="@+id/navigation_sub_item_2"
            android:icon="@drawable/ic_action_person"
            android:title="@string/profile" />
        <item
            android:id="@+id/navigation_sub_item_3"
            android:icon="@drawable/ic_action_labels"
            android:title="@string/tags" />
        <item
            android:id="@+id/navigation_sub_item_4"
            android:icon="@drawable/ic_action_settings"
            android:title="@string/log_in" />
    </group>

</menu>

Here is my layout :

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="@color/white_1000">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar">

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

            <android.support.design.widget.NavigationView
                android:id="@+id/main_drawer"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                app:headerLayout="@layout/drawer_header"
                app:menu="@menu/drawer_menu" />

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

回答1:


Do you want your "Log In" item to be fixed at the bottom of the Navigation View?

You can do that by nesting additional views/layouts within NavigationView. NavigationView extends FrameLayout, so you can wrap it around child views.

I posted more information on NavigationView footers here. Also see the other answers in that post for more layout options.

Edit

FYI: If you use the "fixed footer" approach, you are basically removing the footer item (e.g. "Log In" in your example) from the menu. That means you have to implement click and other handlers yourself.




回答2:


I think navigation menu works like a listview. So Items will be placed in a list.

If you want one item to be placed at the bottom, you can customise within the NavigationView Tag in layout xml



来源:https://stackoverflow.com/questions/30613799/how-to-position-menu-items-in-navigation-view

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