首页 > 解决方案 > 如何从左到右工具栏api 16

问题描述

我正在使用NavigationView并使用include添加工具栏。我使用并且所有 api 都可以正常工作,但在api 16中它不起作用。android:layoutDirection="rtl"

那么我如何在 api 16 中从左到右工具栏?

抽屉布局.xml

   <?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:fitsSystemWindows="true"
   android:layoutDirection="rtl"
   tools:openDrawer="end">


   <FrameLayout
       android:id="@+id/content_frame"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/appbar" />

   <include
       android:id="@+id/appbar"
       layout="@layout/app_bar_main"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:visibility="visible" />

   <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:layoutDirection="rtl"
       app:headerLayout="@layout/nav_header_main"
       app:menu="@menu/activity_main_drawer" />

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

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".view.activity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:elevation="0dp">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_main"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary" />
    </android.support.design.widget.AppBarLayout>
</LinearLayout>

MainActivity.java

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

标签: androidandroid-toolbarandroid-xmlarabicfarsi

解决方案


不幸的是,它无法android:layoutDirection="rtl"在 API 16 或更低版本中使用。

android:layoutDirection="rtl"最初包含在 API 17 及更高版本中。

如果您想了解更多信息,我绝对建议您阅读这篇文章。

但是编程总是有解决方案的。

这个问题与您的问题相似,也许会对您有所帮助。


推荐阅读