Android 从右到左 NavigationDrawer 菜单项不是 RTL
Android right to left NavigationDrawer menu items aren't RTL
我需要将导航菜单项右对齐。
我阅读了很多文章和问答,但我找不到我的项目有什么问题。
这是我的 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 的元素仍然保持从左到右。你可以在这里看到结果:
如您所见,菜单项不是从右到左排列的。如何使它们成为 rtl?
尝试添加 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"。这工作正常。
First delete app:menu="@menu/activity_main_drawer" and in
"@layout/nav_header_main" layout you can set custom layout.in the below you can see content of nav_header_main layout.
<?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>
我需要将导航菜单项右对齐。 我阅读了很多文章和问答,但我找不到我的项目有什么问题。 这是我的 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 的元素仍然保持从左到右。你可以在这里看到结果:
如您所见,菜单项不是从右到左排列的。如何使它们成为 rtl?
尝试添加 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"。这工作正常。
First delete app:menu="@menu/activity_main_drawer" and in "@layout/nav_header_main" layout you can set custom layout.in the below you can see content of nav_header_main layout.
<?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>