2015-11-06 57 views
3

我正在使用android設計支持庫,我想知道如何擁有右側導航抽屜的權利,我將重力設置爲正確但只有導航抽屜本身移動到正確的位置,我想知道如何將菜單項放在右側? 導航視圖:從右到左的導航抽屜菜單使用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

在此先感謝

回答

6

我能得到這個通過添加具有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> 

,然後前setContentView(R.layout.main_activity);添加到您的活動

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

這是唯一可能在API 17+

結果:enter image description here

+0

它在API 21上完美工作,謝謝,但任何其他想法的前API 17? –

+0

@AliSadeghi以前的API我將不得不多花一些時間 –

+0

謝謝你,等待你的回覆... –

0

設置導航視圖的方向。

安卓的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

0

最多投票inner_class7,

不要忘了補充: 機器人:supportsRtl = 「真」在AndroidManifest.xml中。它需要一天的時間才能找到。

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