未在导航视图

时间:2017-01-04 07:19:15

标签: android android-fragments android-menu android-navigation-drawer android-navigationview

我有一个NavigationView&在里面渲染菜单。我的菜单有3组&每个包含很少的子菜单项。我的问题是,当我选择任何子菜单项&转到特定的片段。但是,当我重新打开NavigationView /抽屉选择另一个项目时,它不会显示最后选择的项目突出显示。

目前,它只是更改所选子菜单项的颜色,但我希望它突出显示如下快照。

enter image description here

基本活动XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_app_base"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_app_base"
        app:menu="@menu/activity_app_base_drawer"
        android:background="@color/white"
        app:itemBackground="@color/white" />

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

菜单项XML(activity_app_base_drawer)

<item
    android:id="@+id/nav_android_title"
    android:title="Android" >
    <menu>
        <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_android_best_practices"
            android:icon="@drawable/best_practices_icon"
            android:checked="false"
            android:title="Best Practices" />
        <item
            android:id="@+id/nav_android_monthly_report"
            android:icon="@drawable/report_icon"
            android:checked="false"
            android:title="Monthly Report" />
        </group>
    </menu>
</item>
<item
    android:id="@+id/nav_iOS_title"
    android:title="iOS">
    <menu>
        <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_ios_best_practices"
            android:icon="@drawable/best_practices_icon"
            android:checked="false"
            android:title="Best Practices" />
        <item
            android:id="@+id/nav_ios_monthly_report"
            android:icon="@drawable/report_icon"
            android:checked="false"
            android:title="Monthly Report" />
        </group>
    </menu>
</item>
<item
    android:id="@+id/nav_communicate_title"
    android:title="Other">
    <menu>
        <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_terms"
            android:icon="@drawable/terms_icon"
            android:checked="false"
            android:title="Terms &amp; Conditions" />
        <item
            android:id="@+id/nav_about_us"
            android:icon="@drawable/about_icon"
            android:checked="false"
            android:title="About Us" />
        </group>
    </menu>
</item>

2 个答案:

答案 0 :(得分:4)

您可以使用元素中的android:checkable属性定义单个菜单项的可检查行为,也可以使用元素中具有 android:checkableBehavior 属性的整个组定义可检查行为。因此,将checkableBehavior设置为主菜单

<menu>
<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_android_title"
        android:title="Android" >
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_android_best_practices"
                    android:icon="@drawable/best_practices_icon"
                    android:checked="false"
                    android:title="Best Practices" />
                <item
                    android:id="@+id/nav_android_monthly_report"
                    android:icon="@drawable/report_icon"
                    android:checked="false"
                    android:title="Monthly Report" />
            </group>
        </menu>
    </item>
    <item
        android:id="@+id/nav_iOS_title"
        android:title="iOS">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_ios_best_practices"
                    android:icon="@drawable/best_practices_icon"
                    android:checked="false"
                    android:title="Best Practices" />
                <item
                    android:id="@+id/nav_ios_monthly_report"
                    android:icon="@drawable/report_icon"
                    android:checked="false"
                    android:title="Monthly Report" />
            </group>
        </menu>
    </item>
    <item
        android:id="@+id/nav_communicate_title"
        android:title="Other">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_terms"
                    android:icon="@drawable/terms_icon"
                    android:checked="false"
                    android:title="Terms &amp; Conditions" />
                <item
                    android:id="@+id/nav_about_us"
                    android:icon="@drawable/about_icon"
                    android:checked="false"
                    android:title="About Us" />
            </group>
        </menu>
    </item>
</group>
</menu>

答案 1 :(得分:2)

这是完美代码!

NavigationView

中添加三个atrributes
  

应用程式:itemIconTint
      应用:itemTextColor
      应用程式:itemBackground

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_app_base"
    app:menu="@menu/activity_app_base_drawer"
    android:background="@color/white"
    app:itemIconTint="@drawable/navigation_view_icon_tint_selector"
    app:itemTextColor="@drawable/navigation_view_text_selector"
    app:itemBackground="@drawable/ripple_navigation_selector"
 />

<强> navigation_view_icon_tint_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is used when the Navigation Item is checked -->
<item android:color="#005481" android:state_checked="true" />
<item android:color="#005481" android:state_pressed="true"></item>
<item android:color="#005481" android:state_activated="true"></item>
<!-- This is the default text color -->
<item android:color="#525252" />

<强> navigation_view_text_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is used when the Navigation Item is checked -->
<item android:color="#005481" android:state_checked="true" />
<item android:color="#005481" android:state_pressed="true"></item>
<item android:color="#005481" android:state_activated="true"></item>
<!-- This is the default text color -->
<item android:color="#525252" />

<强> ripple_navigation_selector

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#ffdbdb" android:state_pressed="true"/>
<item android:drawable="#ffdbdb" android:state_activated="true"/>
<item android:drawable="#ffdbdb" android:state_checked="true"/>
<item android:drawable="#FFFFFF"/>