2

我需要將導航菜單項對齊到右側。 我讀了很多文章,問題和答案,但我找不到我的項目中有什麼問題。 這是我的xml:Android從右到左NavigationDrawer菜單項不是RTL

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

<include 
    layout="@layout/content_activity_home" 
    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="right" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_activity_home" 
    app:menu="@menu/activity_home_drawer" /> 

這是activity_home_drawer.xml

<?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/menu_account" 
     android:icon="@drawable/ic_profile" 
     android:title="حساب کاربری" /> 
    <item 
     android:id="@+id/menu_logout" 
     android:icon="@drawable/ic_sign_out" 
     android:title="خروج از حساب" /> 
</group> 

我可以讓自己的抽屜,從正確的,但仍然NavigationView的元素打開保持從左到右。你可以在這裏看到的結果:

enter image description here

正如你看到的菜單項不從右到左。如何讓他們rtl?

+0

我這個代碼添加到我的應用程序,但我得到這個錯誤: –

+0

@ Navid_pdp11「沒有重力LEFT發現抽屜視圖」給我你的代碼來解決它 – Fartab

+0

我加方向RTL到我的抽屜,從右側開現在:) –

回答

6

嘗試添加安卓的layoutDirection = 「RTL」

<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" /> 

這適用於API級別17以上。對於舊設備有一個竅門。 在Android清單的應用程序級別設置supportRtl = 「假」和佈局設置layout_gravity = 「右」。這工作正常。

+0

是的,它的工作原理。謝謝@達爾文。 – Fartab

+0

快樂編碼... – darwin