gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:36 24 4
gpt4 key购买 nike

我需要将导航菜单项右对齐。我阅读了很多文章和问答,但我找不到我的项目有什么问题。这是我的 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?

最佳答案

尝试添加 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 list 的应用程序级别设置 supportRtl="false" 并在布局中设置 layout_gravity="right"。这工作正常。

关于Android 从右到左 NavigationDrawer 菜单项不是 RTL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38848821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com