我一直在尝试通过使用新的来简化一些导航抽屉代码NavigationView https://developer.android.com/reference/android/support/design/widget/NavigationView.htmlAndroid 设计支持库中的类。如果您只想将图标放在左侧,将文本放在右侧(如文档中的示例所示),那么它非常有用,但是如果我想向具有以下内容的布局添加单个自定义视图怎么办?android.support.v7.widget.SwitchCompat
就像在 Google Play 电影应用中一样(参见下面的屏幕截图)?
我尝试过使用actionLayout
属性来指定自定义布局文件,如下面的示例代码所示,但是该属性似乎被忽略,因为它不起作用。
res/menu/navigation_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/group1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_screen1"
android:icon="@drawable/ic_list_black_24dp"
android:title="Screen 1" />
<item
android:id="@+id/nav_screen2"
android:icon="@drawable/ic_search_black_24dp"
android:title="Screen2"/>
</group>
<group android:id="@+id/group2">
<item
android:id="@+id/nav_custom"
android:icon="@drawable/custom_icon_24dp"
app:actionLayout="@layout/nav_drawer_switch"
android:title="Custom item with switch"/>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="Settings"/>
</group>
</menu>
res/layout/nav_drawer_switch.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SwitchCompat
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Custom item with switch"/>
</LinearLayout>
我怎样才能让它发挥作用?理想情况下,我想在仍然使用我的菜单布局的同时添加自定义视图,但如果不使用粗略的黑客(例如利用支持库生成的现有布局)就不可能做到这一点,那么我想知道代码量最少的解决方案,仍然值得过渡到NavigationView
.
The actionLayout
属性是现在 Android 支持库 23.1 支持 http://android-developers.blogspot.jp/2015/10/android-support-library-231.html:
NavigationView 提供了一种构建导航抽屉的便捷方法,包括使用菜单 XML 文件创建菜单项的能力。我们扩展了可能的功能,能够通过 app:actionLayout 或使用 MenuItemCompat.setActionView() 设置项目的自定义视图。
所以问题中的代码现在应该可以工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)