【问题标题】:how to left to right toolbar api 16如何从左到右工具栏api 16
【发布时间】:2018-10-17 10:34:11
【问题描述】:

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

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

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

【问题讨论】:

  • 对 RTL 布局方向的支持已包含在 API 17 中,在此之前它不起作用。没有解决办法。
  • 你可以使用多个布局文件来解决这个问题
  • @RahulKumar 你能解释一下吗?或者给我一个链接。

标签: android android-toolbar android-xml arabic farsi


【解决方案1】:

很遗憾,在 API 16 或更低版本中无法使用 android:layoutDirection="rtl"

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

如果您想了解更多信息,我绝对推荐阅读this 文章。

但编程总有解决办法。

This 的问题和你的类似,也许对你有帮助。

【讨论】:

    【解决方案2】:

    低于 17 的 API 无法支持 rtl Toolbars。尝试使用自定义Toolbar,例如RtlToolbar

    Build.gradle:

    dependencies {
        compile 'com.alirezaafkar:toolbar:1.1.2'
    }
    

    layout.xml:

    <com.alirezaafkar.toolbar.RtlToolbar
        android:id="@+id/toolbar_main"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:direction="rtl"
        app:optionsMenu="@menu/menu_main"
        app:font="@string/font_path"
        app:navigationIcon="@drawable/ic_menu"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="@string/app_name"/>
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-05
      • 2016-03-03
      • 2016-05-02
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多