2016-02-14 46 views
2

我要构建波斯语应用(RTL)。我的应用程序包括一个导航抽屉。API 17中的Android布局方向问题

我在清单中加入应用标签机器人:supportsRtl = “真”

和在onCreate()方法:。getWindow()getDecorView()setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

我知道LayoutDirection小于17 api不起作用。

但图标(工具栏& NavigationDrawer)在API 17是相反的:

:在API 17(不正确)

enter image description here

结果:API 22(正确)

结果

enter image description here

我该如何解决这个问题?

这里是我的布局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
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:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="ir.mhborujerdi.mhb.ebook.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <include layout="@layout/content_main" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 
+0

可以把XML的刷卡布局 – 2016-02-14 08:42:32

+0

@OnurTaş把它。 –

回答

0

具有API是最低17的要求是有点昂贵的,因为它是不是真的老沿用至今...

我想建议从右侧拉抽屉的另一种方式。

所以,在你的XML文件,你需要更改部分,

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 

如果设置android:layout_gravity="start""right",菜单会从右侧打开,并可以通过始终定向右侧设计XML 。

通过这种方式,您不必在onCreate()上设置布局,并且API不会受到17+限制。

工具栏

而不是默认的动作条

更新,如棒棒糖我们开始了自己的Toolbar的代码,你可以把你自己的工具栏由你决定了整个设计。工具栏例如,穆罕默德,我敢肯定,你可以通过简单的搜索

+0

谢谢。但与此解决方案运行时错误已发生: java.lang.IllegalArgumentException:没有找到重力抽屉视图LEFT at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1464) –

4

此问题针对前一段时间发现绰绰有余,但如果别人需要针对此问题的解决方案,这将解决这个问题

ViewCompat.setLayoutDirection(mRoot , ViewCompat.LAYOUT_DIRECTION_LTR); 

我希望它可以帮助别人..

+0

感谢它的工作正常。 –