如何在API 16中使工具栏从左到右显示

12

我正在使用 NavigationView 并添加了带有 includetoolbar。我使用了 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

->

主活动.java

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

2
API 17 中包含了对 RTL 布局方向的支持,此前版本不支持。目前还没有解决方案。 - Ernest Zamelczyk
你可以使用多个布局文件来解决这个问题。 - Rahul
@RahulKumar,你能否再解释一下或者给我一个链接? - Hamed Karami
2个回答

7
很遗憾,API 16或更低版本无法使用android:layoutDirection="rtl"android:layoutDirection="rtl"是在API 17及以上版本中首次引入的。
如果您想了解更多信息,我建议阅读这篇文章
但在编程中总是有解决方案的。 这个问题与您的类似,也许能帮到你。

2

在API 17以下的版本中,无法支持从右到左的工具栏。尝试使用自定义工具栏,例如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"/>

结果:

在此输入图像描述


这段内容涉及IT技术,展示了一张图片。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接