Android从右到左的NavigationDrawer菜单项不是RTL

时间:2016-08-09 10:55:40

标签: android xml user-interface right-to-left android-navigation-drawer

我需要将导航菜单项对齐到右侧。 我阅读了很多文章,问题和答案,但我无法找到项目中的错误。 这是我的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="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?

2 个答案:

答案 0 :(得分:8)

尝试添加android: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 =“false” ,并在布局设置 layout_gravity =“right” 。这很正常。

答案 1 :(得分:0)

  

首先删除app:menu =“ @ menu / activity_main_drawer”,然后在   您可以设置“ @ layout / nav_header_main”布局的自定义布局。在下面,您可以看到nav_header_main布局的内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/nav_header_height"
        android:background="@drawable/banner"
        android:gravity="right">



    </LinearLayout>

</LinearLayout>
相关问题