从右到左使用Android设计支持库的导航抽屉菜单 [英] Right to left navigation drawer menu using android design support library

查看:100
本文介绍了从右到左使用Android设计支持库的导航抽屉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android设计支持库,我想知道我如何才能从右向左导航抽屉,我将重力设置为右,但是只有导航抽屉本身向右移动,我想知道如何我把菜单项放在右边吗? 导航视图:

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

抽屉布局:

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

这是我的抽屉:

预先感谢

推荐答案

我能够通过添加如下所示的DrawerLayout使它起作用:

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>

,然后在之前 setContentView(R.layout.main_activity);之前添加到您的活动:

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);
    }

这仅适用于API 17 +

This is only possible on API 17+

结果:

这篇关于从右到左使用Android设计支持库的导航抽屉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆