【问题标题】:Right to left navigation drawer menu using android design support library使用android设计支持库的从右到左的导航抽屉菜单
【发布时间】:2015-11-06 21:37:36
【问题描述】:

我正在使用 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>

这是我现在的抽屉:

提前致谢

【问题讨论】:

    标签: android navigation right-to-left


    【解决方案1】:

    我可以通过添加 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+ 上实现

    结果:

    【讨论】:

    • 它在 API 21 上完美运行,谢谢,但是对于 pre API 17 还有其他想法吗?
    • @AliSadeghi 对于以前的 API 我将需要更多时间
    • 还有别的,有没有办法在这个导航抽屉里有一个回收站视图而不是菜单项?
    • @AliSadeghi RecyclerView 可以在抽屉布局中没有问题。这是一个很好的教程链接,可以帮助android4devs.com/2014/12/…
    • 这个我知道了,想知道有没有办法在这个抽屉里添加RecyclerView,顺便谢谢
    【解决方案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" />
    

    enter image description here

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        只需添加 navigationView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 对齐所有图标的这条线在右侧 并且不要忘记添加 android:supportsRtl="true" 此行进入应用程序内的清单。

        【讨论】:

          猜你喜欢
          • 2015-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-02
          • 1970-01-01
          • 1970-01-01
          • 2015-08-29
          • 1970-01-01
          相关资源
          最近更新 更多