LesMiserables25 LesMiserables25 - 15 days ago 4
Android Question

Android - Gravity of Toolbar title isn't correct

I'm trying to style the Toolbar in AppCompat, but the title goes on the top in the Toolbar.

In other Activities, this problem doesn't happen.

How to fix it?

image


  • OS: Android 6.0.1

  • Device: Nexus 5




styles.xml


<style name="AppTheme.Black" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">#304FFE</item>
<item name="android:windowContentOverlay">@null</item>
<item name="preferenceTheme">@style/AppTheme.Preference</item>
<item name="alertDialogTheme">@style/AppAlertDialog.Black</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowAnimationStyle">@style/AppTheme.Transition</item>
<item name="wcToolbarTheme">@style/AppTheme.Toolbar.Black</item>
<item name="wcDefaultPreferenceTextColor">#000000</item>
<item name="wcMenuTintColor">#ffffff</item>
<item name="dialogTheme">@style/AppAlertDialog.Black</item>
<item name="toolbarStyle">?attr/wcToolbarTheme</item>
<!-MaterialDrawer->
<item name="material_drawer_background">?android:attr/windowBackground</item>
<item name="material_drawer_primary_text">@color/black</item>
<item name="material_drawer_selected_text">?attr/material_drawer_primary_text</item>
</style>

<style name="AppTheme.Toolbar.Black" parent="Widget.AppCompat.Toolbar">
<item name="colorControlNormal">#ffffff</item>
<item name="titleTextColor">?attr/colorControlNormal</item>
</style>

<style name="AppTheme.NoActionBar.Black" parent="AppTheme.Black">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="preferenceTheme">@style/AppTheme.Preference</item>
</style>



Code fragment of Toolbar in the layout


<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_scrollFlags="scroll|enterAlways"
style="?attr/toolbarStyle"
app:theme="?attr/toolbarStyle"
android:theme="?attr/toolbarStyle"/>

Answer

My Solution: Setting the Gravity of a TextView for the title to CENTER in Java code.