使用Android设计支持库实现从右向左的导航抽屉菜单

4

我正在使用Android设计支持库,想知道如何实现从右到左的导航抽屉菜单。我将重力设置为右侧,但只有导航抽屉本身移动到了右侧。我想知道如何将菜单项放在右侧?

导航视图:

    <android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:foregroundGravity="right"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer" />

抽屉布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
android:layout_gravity="right">

菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/one"
        android:checked="false"
        android:icon="@drawable/account"
        android:title="First Item"></item>

    <item
        android:id="@+id/two"
        android:checked="false"
        android:icon="@drawable/filter"
        android:title="Second Item"></item>

    <item
        android:id="@+id/three"
        android:checked="false"
        android:icon="@drawable/human"
        android:title="Third Item"></item>
</group>

这是我的抽屉现在的样子: Drawer 谢谢。
4个回答

9

我通过以下方式将DrawerLayout添加到代码中,使其正常运行:

<?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:gravity="right"
     android:fitsSystemWindows="true" 
     tools:openDrawer="right">

     <include 
         layout="@layout/app_bar_main" 
         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_main"       
         app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

然后在您的Activity中,在setContentView(R.layout.main_activity); 之前添加:

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    }

这只能在API17+上实现。

结果:图片描述


它在API 21上完美运行,谢谢,但是有没有其他关于API 17之前的想法? - Ali Sadeghi
@AliSadeghi 对于之前的API,我需要再花些时间。 - kandroidj
还有其他的东西,有没有办法在这个导航抽屉中使用RecyclerView而不是菜单项? - Ali Sadeghi
@AliSadeghi RecyclerView 可以轻松放置在抽屉布局中。这里有一个好的教程链接,可以帮助你:http://www.android4devs.com/2014/12/how-to-make-material-design-navigation-drawer.html - kandroidj
我知道这个,我想知道是否有任何方法在这个抽屉中添加RecyclerView,顺便说一句,谢谢。 - Ali Sadeghi
如何在此菜单图标的左侧添加溢出菜单项? - CGR

2
设置导航视图的方向。
android:layoutDirection="rtl"
 <android.support.design.widget.NavigationView
    android:layoutDirection="rtl"
    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_main"
    app:menu="@menu/activity_main_drawer" />

输入图像描述


1

给inner_class7点个赞,

别忘了在AndroidManifest.xml中添加:android:supportsRtl="true"。找了半天才发现。

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

-1
只需添加navigationView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);这一行代码即可将所有图标对齐到右侧,不要忘记在应用程序清单中添加android:supportsRtl="true"这一行代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接