如何在Android的导航抽屉中添加可折叠菜单项? [英] How to add a collapsible menu item inside navigation drawer in android?
问题描述
我有一个DrawerLayout
,其中包含一个NavigationView
,并且此布局活动用作应用程序中所有活动的通用导航抽屉.我在导航视图中提供app:menu
的菜单资源.我有一些菜单项,但是我希望一个菜单项是可折叠/可扩展的,因此当我单击它时,它会展开以显示两个子菜单,并在第二次单击时再次折叠.
I have a DrawerLayout
enclosing a NavigationView
and this layout activity serves as a common Navigation drawer for all the activities in my app. I am providing the menu resource for app:menu
in navigation view. I have some menu items, but I want one menu item to be collapsible/expandable, so that when I click on it, it expands to show two submenus and collapses again on a second click.
我通过在<item>
内添加另一个<menu>
来添加子菜单,但无法使其可折叠/展开.
I have added submenus by adding another <menu>
inside the <item>
but cant make it collapsible/expandable.
此外,我不想出于自己的目的使用ExpandableListView
.相反,我只需要在菜单资源文件中进行一些调整即可.请给我指出正确的方向.我搜索Google只是为了使用ExpandableListView
查找可折叠列表项的代码,博客和示例,但是我需要它与设计支持库中的NavigationView
设计小部件一起使用.
Additionally, I don't want to use ExpandableListView
for my purpose. Instead, I just need to do some tweaks in menu resource file. Please just point me in the correct direction. I have searched Google only to find code, blogs, and examples for collapsible list items using ExpandableListView
, but I need it to work with the NavigationView
design widget from the design support Library.
这是我的菜单文件代码:
Here's my code for the menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_aboutus"
android:title="About Us" />
<item
android:id="@+id/nav_faq"
android:title="FAQs" />
<item
android:id="@+id/nav_share"
android:title="Share" />
<item
android:id="@+id/nav_myaccount"
android:title="My Account" />
<item
android:id="@+id/nav_legal"
android:title="Legal" >
<menu>
<item
android:id="@+id/nav_tnc"
android:title="Terms and Conditions" />
<item
android:id="@+id/nav_pp"
android:title="Privacy Policy" />
</group>
</menu>
</item>
</menu>
我希望法律"菜单项可扩展为具有两个子菜单项条款和条件",隐私权政策".
I want Legal menu item to be expandable having two submenu items 'Terms and Conditions', 'Privacy Policy'.
推荐答案
您可以在导航抽屉中使用可扩展列表视图,但我不明白为什么您不想使用它们.有关更多信息,请参见 http://developer.android.com/reference/android/widget/ExpandableListView.html
You can use expandable list views inside navigation drawers, I don't understand why you don't want to use them. More on that can be found here http://developer.android.com/reference/android/widget/ExpandableListView.html
如果您坚持不使用expandableListView,那么另一种方法是使用菜单的OnSelect特定项自己设计扩展.尽管我真的不知道为什么要这么做,但是您只是重新实现了.
If you insist on not using expandableListView, then the alternative is to design the expansion yourself using the menu's OnSelect for that specific item. Though I really don't know why you want this, you'd just be re-implementing the wheel.
这篇关于如何在Android的导航抽屉中添加可折叠菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!