代码之家  ›  专栏  ›  技术社区  ›  Hamed Karami

如何从左到右工具栏api 16

  •  10
  • Hamed Karami  · 技术社区  · 6 年前

    我在用 加上 具有 . 我用 android:layoutDirection="rtl" 所有的api都工作得很好,但是 api 16标准 它不起作用。

    那么如何在API16中使用从左到右的工具栏呢?

    抽屉布局.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>
    

    main活动.java

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
    
    2 回复  |  直到 6 年前
        1
  •  6
  •   Jakob    5 年前

    不幸的是它不能使用 android:layoutDirection="rtl"

    android:layoutDirection=“rtl”

    如果你想知道更多的信息,我绝对建议你读 this 文章。

    This 这个问题和你的问题很相似,也许会对你有帮助。

        2
  •  1
  •   aminography    6 年前

    无法支持rtl Toolbar 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"/>
    

    结果:

    enter image description here