Right to left menu items Android in NavigationView

2020-02-14 06:28发布

问题:

im beginer of android. i design a navigation drawer like this:

as you see all items in menu are on the right Except "SubHeader".

how can i move "SubHeader" item to the right of this menu? i use all properties like gravity, layout gravity, layoutDirection to the SubHeaderItem but it doesn't work.

my code is

 <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigation_view"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        android:layoutDirection="rtl"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_menu"
        />

my menu code is:

<menu xmlns:android="http://schemas.android.com/apk/res/android"    >
    <group android:checkableBehavior="single">
        <item
            android:title="inbox"
            android:id="@+id/inbox_id"
            android:icon="@drawable/inbox" />
    </group>
    <item android:title="SubHeader">

        <menu>
            <item
                android:title="all main"
                android:id="@+id/all_mail"
                android:icon="@drawable/allmail" />
        </menu>
    </item>

</menu>

回答1:

Try adding these attributes to NavigationView in your layout file.

android:layoutDirection="rtl"
android:textDirection="rtl"

If no other way. You should use custom layout

example:

<android.support.design.widget.NavigationView
  android:layoutDirection="rtl"
  android:id="@+id/navigation_view"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_gravity="right"
  android:fitsSystemWindows="true"
  app:headerLayout="@layout/nav_header_main"
  app:menu="@menu/activity_main_drawer>
 <LinearLayout>
 // code xml here
 </LinearLayout>
 <android.support.design.widget.NavigationView/>


回答2:

try this make your menu gravity="right" of your menu item

<menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:gravity="end"
 android:layoutDirection="rtl"
 android:layout_gravity="end">

 <group android:checkableBehavior="single"
    android:layout_gravity="end"
    android:gravity="end">

    <item
        android:id="@+id/nav_1"
        android:icon="@drawable/abc"
        android:gravity="end"
        android:layout_gravity="end"
        android:title="menu title"/>
     </group>
</menu>

and make your navigation view like this

<android.support.design.widget.NavigationView
      android:layoutDirection="rtl"
      android:id="@+id/navigation_view"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="right"
      android:fitsSystemWindows="true"
      app:headerLayout="@layout/nav_header_main"
      app:menu="@menu/activity_main_drawer" />