如何添加导航视图中的新的Android支持库 [英] How to add a navigation view in the new android support library
问题描述
我在我的布局做这个:
< android.support.v4.widget.DrawerLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fitsSystemWindows =真正的> <! - 你的内容布局 - > < android.support.design.widget.NavigationView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =match_parent
机器人:layout_gravity =开始
应用:headerLayout =@布局/ drawer_header
应用:菜单=@菜单/抽屉/>
< /android.support.v4.widget.DrawerLayout>
和这在我的菜单:
<项目
机器人:ID =@ + ID / navigation_subheader
机器人:标题=@字符串/ navigation_subheader>
<菜单>
<项目
机器人:ID =@ + ID / navigation_sub_item_1
机器人:图标=@绘制/ ic_android
机器人:标题=@字符串/ navigation_sub_item_1/>
<项目
机器人:ID =@ + ID / navigation_sub_item_2
机器人:图标=@绘制/ ic_android
机器人:标题=@字符串/ navigation_sub_item_2/>
&所述; /菜单>
< /项目>
第一个问题,如果我这样做,我不能看到导航抽屉中的所有元素。第二个问题,如果我想补充的头,我可以看到it.For头,我只是puttin alyout谁拥有红色背景的亲戚。
问题1
块引用>您菜单需要有
<菜单>
作为根元素,像这样的:<菜单>
<项目
机器人:ID =@ + ID / navigation_item_1
机器人:图标=@绘制/ ic_android
机器人:标题=@字符串/ navigation_item_1/>
<项目
机器人:ID =@ + ID / navigation_item_2
机器人:图标=@绘制/ ic_android
机器人:标题=@字符串/ navigation_item_2/>
&所述; /菜单>如果你真的想使用的子菜单,然后将另一个
<菜单>
顶级放在<项目>
。请参见菜单资源关于这些如何嵌套元素的详细信息。
问题#2
块引用>检查
layout_width
和layout_height
。此外,请确保它在你的IDE布局编辑器中正确显示第一次。更新:
如果您的导航抽屉你的动作条的下方画(如你在评论中提到的),它关系到你的布局,而不是你的菜单项。请发表你如何创造你的动作条(在code或XML)。
I'm doing this in my layout :
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <!-- your content layout --> <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/drawer_header" app:menu="@menu/drawer"/> </android.support.v4.widget.DrawerLayout>
and this in my menu :
<item android:id="@+id/navigation_subheader" android:title="@string/navigation_subheader"> <menu> <item android:id="@+id/navigation_sub_item_1" android:icon="@drawable/ic_android" android:title="@string/navigation_sub_item_1"/> <item android:id="@+id/navigation_sub_item_2" android:icon="@drawable/ic_android" android:title="@string/navigation_sub_item_2"/> </menu> </item>
First problem if I do this I can't see all the elements of the navigation drawer. Second problem if I want to add an header I can see it.For the header I'm only puttin a relative alyout who have a red background.
解决方案Problem #1
Your menu needs to have
<menu>
as the root element, like this:<menu> <item android:id="@+id/navigation_item_1" android:icon="@drawable/ic_android" android:title="@string/navigation_item_1"/> <item android:id="@+id/navigation_item_2" android:icon="@drawable/ic_android" android:title="@string/navigation_item_2"/> </menu>
If you really want to use submenus, then place another
<menu>
inside of a top-level<item>
. See Menu Resource for more info on how to nest these elements.Problem #2
Check your
layout_width
andlayout_height
. Also, make sure it displays correctly in your IDE layout editor first.Update:
If your navigation drawer is drawing underneath your action bar (like you mentioned in the comments), it's related to your layout, not your menu items. Please post how you are creating your action bar (either in code or in xml).
这篇关于如何添加导航视图中的新的Android支持库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!