5
I’m trying to center the title of mine Toolbar
but I can’t.
I’ve tried to use Theme
(did not work) and I have tried in Java code (however in Java code as I am using the API 13, can’t use the .setTextAligment
). I’ve also tried using getSupportActionBar().setDisplayOptions(toolbar.TEXT_ALIGNMENT_CENTER)
which also did not work.
I don’t have any 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>
xml layout.
<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>
Have you tried to set your
style
with the attributeapp:theme
?– Wakim
@sicachester, last commentator pf
– porthfind
Comment on sicachester’s reply, or it will not be notified.
– bfavaretto
@bfavaretto comment here because below when typing user_name then does not appear to me
– porthfind
The system removes @user because it is unnecessary there (the answer is his, he is notified of all comments). Already here, as he is not in this "conversation" (no comment), does not receive any notification (nor with
@
). See http://meta.pt.stackoverflow.com/questions/943/– bfavaretto