API 17中的Android布局方向问题

时间:2016-02-14 08:36:52

标签: android android-layout

我要构建一个波斯应用程序(RTL)。我的应用程序包括导航抽屉。

我在Manifest中添加了应用程序标签android:supportsRtl =“true”

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

我知道少于17个api的LayoutDirection不起作用。

但是api 17中的图标(Toolbar& NavigationDrawer)是相反的:

API 22中的结果(正确):

enter image description here

API 17中的结果(不正确):

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>

3 个答案:

答案 0 :(得分:4)

此问题前一段时间被问过,但如果其他人需要针对此问题的解决方案,这将解决此问题

ViewCompat.setLayoutDirection( mRoot , ViewCompat.LAYOUT_DIRECTION_LTR );

我希望它对某人有所帮助..

答案 1 :(得分:0)

having the requirement that API is minimum 17 is a little costly since it is not really old and still in use today...

I would like to suggest another way of pulling navigation drawer from right.

So, in your xml file you need to change that part,

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

if you set android:layout_gravity="start" to "right", menu will be opened from right, and you can design the xml by orienting the right side always.

By this way you don't have to set the layout on onCreate() and API is not gonne bounded by 17+.

update for toolbar

instead of the default actionbar, as of lollipop we started to code our own Toolbar and you can put your own toolbar with the whole design determined by you. for toolbar example, Mohammad, I am pretty sure you can find more than enough by simple search

答案 2 :(得分:0)

您可以在NavigationView的XML中使用以下代码:

    android:layoutDirection="rtl"
    android:textDirection="rtl"

我希望它可以帮助某人。