I have a toolbar and i want to align the text to left instead of right.
The Main.xml file code is:
<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:
<?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:
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
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. I guess you are asking to override the RTL behaviour of your text.
In order to make your text left align add this attribute to your Toolbar
android:layoutDirection = "ltr"
<?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:
<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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.