Essayez de centrer un TextView dans la barre d'outils Android, mais lorsque vous ajoutez une vue à la pile arrière, vous avez perdu le centrage. Apparaît que l'élément arrière ne fait pas partie de la barre d'outils, ce qui provoque le problème.Comment centrer le texte dans la barre d'outils Android AppCompat lorsque l'activité est ajoutée à la pile arrière?
Voici le code XML pour la barre d'outils:
<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_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways">
<FrameLayout
android:background="@color/primary"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This is a centered title -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginLeft="?attr/actionBarSize"
android:layout_marginRight="?attr/actionBarSize"
android:gravity="center_vertical|center_horizontal">
<TextView
android:id="@+id/toolbar_title"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Blah"
android:textSize="18dp"
android:textColor="@color/white"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse" />
</LinearLayout>
</FrameLayout>
En l'absence de pile arrière est centrée:
Lorsque l'activité ajoutée à l'arrière pile montre le bouton de retour et le texte le centrage est perdu:
Avez-vous essayé d'utiliser une vue personnalisée pour la barre d'outils? – Arslan