Android Toolbar: center title

5

I'm trying to center the title of my Toolbar but I can not.

I've already tried using Theme (it did not work) and I already tried it in Java code (however in Java code as I'm using API 13 , I can not use .setTextAligment ). I've also tried to use getSupportActionBar().setDisplayOptions(toolbar.TEXT_ALIGNMENT_CENTER) which also did not work.

I have no more ideas ...

styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>
    <style name="AppTheme.Toolbar.Title" parent="Theme.AppCompat.NoActionBar">
        <!-- Set proper title size -->
        <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_gravity">center</item>
    </style>
</resources>

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow0"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <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/toolbar"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="?attr/actionBarSize"
        android:background="@drawable/toolbar_main">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/brown"
            android:textSize="16sp"
            android:textStyle="bold"
            android:gravity="center"
            android:id="@+id/toolbar_title"/>

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

    <LinearLayout
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:gravity="center"
        android:layout_gravity="center"
        android:paddingTop="70dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/buttonMesActual"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:textColor="@color/brown"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:background="@drawable/buttons"
            android:text="Botão1"
            android:layout_marginBottom="10dp"
            android:onClick="onClick"/>

        <Button
            android:id="@+id/buttonInserir"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:background="@drawable/buttons"
            android:text="Botão2"
            android:textColor="@color/brown"
            android:layout_marginBottom="10dp"
            android:onClick="onClick"/>

        <Button
            android:textColor="@color/brown"
            android:id="@+id/buttonMessPassados"
            android:layout_width="200dp"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content"
            android:background="@drawable/buttons"
            android:text="Botão3"
            android:onClick="onClick"/>

        <Button
            android:background="@drawable/buttons"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/buttonDefinirM"
            android:textColor="@color/brown"
            android:text="Botão4"
            android:onClick="onClick"/>

    </LinearLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_gravity="bottom|center"
        android:gravity="bottom|center"
        android:layout_height="wrap_content"
        android:paddingTop="40dp"
        android:orientation="vertical">

        <TableRow android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:textStyle="bold"
                android:id="@+id/textMesActual"
                android:text="Text2:"
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textMes"
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:textStyle="bold"
                android:textColor="@color/brown"
                android:id="@+id/textDespesas"
                android:text="Text1:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView

                android:id="@+id/textDesp"
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:textStyle="bold"
                android:id="@+id/textMeta"
                android:text="Text0: "
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textMetaDef"
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>
</LinearLayout>

toolbar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape android:shape="rectangle">
            <gradient android:angle="-90" android:startColor="#fdeec1" android:endColor="#f9d35e"  />

        </shape>
    </item>
</layer-list>
    
asked by anonymous 06.03.2015 / 00:21

2 answers

3

Remember that Toolbar is ViewGroup , that is, you can put components inside it without any problem.

To put text in the center, put a TextView as the android:gravity="center" attribute:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/seuStyle"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Seu titulo"
        android:textSize="16sp" />

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

Just one important detail I noticed in your layout: use the integer that is in AppCompat called ?attr/actionBarSize to set the size of your Toolbar . Will apply the default and correct size for your Toolbar

Ref: link

@Edit

In your layout, TextView is as android:layout_width="wrap_content" and android:layout_height="wrap_content" . Leave as match_parent and it will work

    
06.03.2015 / 14:55
0

My opinion and do not try to put the text in this way because it does not meet the defined placement and spacing specifications here .

Always try to follow the guidelines and you'll find that it will make an app more easily and high quality.

    
06.03.2015 / 10:54