Right to left navigation drawer menu using android design support library

北城余情 提交于 2020-01-02 06:17:40

问题


I'm using the android design support library and i want to know how can i have a right to left navigation drawer,I set the gravity to right but only the navigation drawer itself moved to right, I want to know how can i put the menu items on the right side ? Navigation View :

    <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" />

Drawer layout :

<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 :

<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>

and this is my drawer now :

thanks in advance


回答1:


I was able to get this to work by adding having the DrawerLayout as below:

<?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>

and then add to your Activity before setContentView(R.layout.main_activity);:

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

This is only possible on API 17+

Result:




回答2:


set direction for navigation view .

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" />

enter image description here




回答3:


Up vote for inner_class7,

Don't forget to add : android:supportsRtl="true" in AndroidManifest.xml. It toke half of day to find.

<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>


来源:https://stackoverflow.com/questions/33576104/right-to-left-navigation-drawer-menu-using-android-design-support-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!