2016-03-07 119 views
2

如何在導航抽屜中加下劃線菜單項?抽屜式導航列表中的下劃線菜單項

預期

enter image description here

現實

enter image description here

@菜單/ activity_navigation_drawer:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

<group android:checkableBehavior="single"> 
    <item 
     android:id="@+id/nav_open_account" 
     android:title="@string/na_navigate_open_account" /> 
    <item 
     android:id="@+id/nav_login" 
     android:title="@string/na_navigate_login" /> 
    <item 
     android:id="@+id/nav_stock_signals" 
     android:title="@string/na_navigate_stock_signals" /> 

    ... 

</group> 

導覽活動佈局:

<?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="end"> 

<include 
    layout="@layout/app_bar_navigation" 
    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="end" 
    android:fitsSystemWindows="true" 
    android:background="@color/primary_asphalt_dark" 
    app:headerLayout="@layout/nav_header_navigation" 
    app:menu="@menu/activity_navigation_drawer" /> 

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

告訴我,如果更多的代碼需要。

回答

5

每組以分隔符結束。因此,如果在你的菜單中的每個項目都有自己的小組,你會達到預期的圖形輸出:

<?xml version="1.0" encoding="utf-8"?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <group android:id="@+id/nav_open_account_group" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_open_account" 
      android:title="@string/na_navigate_open_account" /> 
     </group> 
    <group android:id="@+id/nav_login_group" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_login" 
      android:title="@string/na_navigate_login" /> 
    </group> 
    <group android:id="@+id/nav_stock_signals_group" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_stock_signals" 
      android:title="@string/na_navigate_stock_signals" /> 
    </group> 

     ... 

    </menu> 

enter image description here

請注意,每個組不同的ID都需要這個工作。從com.android.support:design-26.1.0

將文件複製

+0

太多的感謝!奇蹟般有效 – AnZ

0

按兩次Shift

打開文件design_navigation_menu_item.xmldesign_navigation_menu_item.xml\app\src\main\res\layout覆蓋它

添加android:paddingStart="@dimen/design_navigation_icon_padding"CheckedTextView,使文件看起來是這樣的:

<merge xmlns:android="http://schemas.android.com/apk/res/android"> 

<CheckedTextView 
     android:id="@+id/design_menu_item_text" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:drawablePadding="@dimen/design_navigation_icon_padding" 
     android:paddingStart="@dimen/design_navigation_icon_padding" 
     android:gravity="center_vertical|start" 
     android:maxLines="1" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body2"/> 

<ViewStub 
     android:id="@+id/design_menu_item_action_area_stub" 
     android:inflatedId="@+id/design_menu_item_action_area" 
     android:layout="@layout/design_menu_item_action_area" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent"/> 

</merge> 

In您activity_main.xml添加到您的NavigationViewapp:itemBackground="@drawable/drawer_item_background_selector",它應該是這樣的:

<android.support.design.widget.NavigationView 
     ... 
     app:itemBackground="@drawable/drawer_item_background_selector" 
     .../> 

創建drawable/drawer_item_background_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@color/drawer_item_background_selected" android:state_pressed="true" /> 
    <item android:drawable="@color/drawer_item_background_selected" android:state_checked="true" /> 
    <item android:drawable="@color/drawer_item_background_selected" android:state_focused="true" /> 
    <item android:drawable="@color/drawer_item_background_selected" android:state_activated="true" /> 
    <item android:drawable="@drawable/drawer_item_background_default" /> 
</selector> 

創建drawable/drawer_item_background_default.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@color/drawer_transparent" /> 
    <stroke 
     android:width="1dp" 
     android:color="@color/appGrayLight" /> 
    <padding 
     android:bottom="1dp" 
     android:left="-2dp" 
     android:right="-2dp" 
     android:top="-2dp" /> 
</shape> 

enter image description here