【问题标题】:Android right to left NavigationDrawer menu items aren't RTLAndroid 从右到左 NavigationDrawer 菜单项不是 RTL
【发布时间】:2016-08-09 10:55:40
【问题描述】:

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

【问题讨论】:

  • 我将此代码添加到我的应用程序中,但我收到此错误:“没有找到重力左侧的抽屉视图”
  • @Navid_pdp11 将您的代码发送给我以修复它
  • 我将 Direction RTL 添加到我的抽屉,它从现在开始打开 ​​:)

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


【解决方案1】:

尝试添加 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" 中。这可以正常工作。

【讨论】:

    【解决方案2】:

    首先删除 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 2016-09-16
      • 2014-12-16
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多