简体   繁体   English

工具栏中从左到右对齐

[英]Left to right alignment in Toolbar

I have a toolbar and i want to align the text to left instead of right.我有一个工具栏,我想将文本左对齐而不是右对齐。

The Main.xml file code is: Main.xml 文件代码为:

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/main_page_toolbar"
            layout="@layout/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        </include>

The code of app_bar_layout.xml is: app_bar_layout.xml 的代码是:

   <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/grey_border_bottom"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:titleTextColor="@color/link_blue">

    </android.support.v7.widget.Toolbar>

And the relevant code in Main java file is: Main java文件中的相关代码是:

  mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
        setSupportActionBar(mToolbar);
        final String phoneLanguage = Locale.getDefault().toString();
        if (phoneLanguage.equals("iw_IL"))
        {
            getSupportActionBar().setTitle("פוליטיקה");
        }
        else
        {
            getSupportActionBar().setTitle("Politico");
        }

Do not set title Just add the TextView in Toolbar不设置标题 只需在工具栏中添加 TextView

Here is demo这是演示

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


    </android.support.v7.widget.Toolbar>

Since your text is in Hebrew the Toolbar by default supports RTL for all RTL supported languages.由于您的文本是希伯来语,因此工具栏默认支持所有 RTL 支持语言的 RTL。 I guess you are asking to override the RTL behaviour of your text.我猜您是在要求覆盖文本的 RTL 行为。

In order to make your text left align add this attribute to your Toolbar为了使您的文本左对齐,将此属性添加到您的Toolbar

android:layoutDirection = "ltr"

include Textview in your app_bar_layout.xml在 app_bar_layout.xml 中包含 Textview

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:textColor="#000000"
            android:text="@string/app_name"
            android:gravity="right"
            />
    </android.support.v7.widget.Toolbar>

If your entire app should be in RTL, you should go to styles.xml and add this to the AppTheme:如果你的整个应用程序应该在RTL,你应该去styles.xml ,这增加了AppTheme:

 <item name="android:layoutDirection">rtl</item>

And, of course, do not forget to add android:supportsRtl="true" under the application tag in your AndroidManifest.xml file.当然,不要忘记在AndroidManifest.xml文件的application标签下添加android:supportsRtl="true"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM