【发布时间】:2015-08-24 01:58:22
【问题描述】:
我一直在尝试通过使用 Android 设计支持库中的新 NavigationView 类来简化一些抽屉式导航代码。如果您只想要左侧的图标和右侧的文本(如文档中的示例),它会很好用,但如果我想在布局中添加一个自定义视图,其中有一个 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>
我怎样才能让它工作?理想情况下,我想在仍然使用我的菜单布局的同时添加自定义视图,但如果不使用粗略的 hack (比如利用支持库生成的现有布局)这是不可能的,那么我想知道代码量最少的解决方案,仍然值得转换到NavigationView。
【问题讨论】:
-
您无法使用当前 NavigationView 实现自定义项目。 NavigationView 使用 ListView 来填充作为 TextView 的项目,目前无法自定义该 ListView 项目。
-
有没有办法在菜单项中添加 2 行文本?我尝试通过 actionLayout 添加,并将其添加到右侧而不是下方。 Google drive-> Upgrade storage option 等示例显示 2 行文本