2

试图在 Android 工具栏中将 TextView 居中,但是当有一个视图添加到后台堆栈时,会失去居中。似乎后面的项目不是工具栏的一部分,这会导致问题。

这里是工具栏的 xml:

<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>

当没有后堆栈居中时:

工具栏中的居中 TextView

当添加到后退堆栈的 Activity 显示后退按钮并且文本居中丢失时:

Activity 添加到后台堆栈时丢失居中

4

1 回答 1

1

问题是对您所拥有的中心 TexView 的误解。

TextView 位于其父亲(LinearLayout)的中心,但不在Toolbar 的中心

当您将新项目添加到工具栏(导航项目)时,它会推动 FrameLayout,但 TextView 保持在其父 LinearLayout 的中心,但不在工具栏的中心

TextView 是LinearLayout 的一项,位于LinearLayout 容器的中心。

LinearLayout 是 FrameLayout 的一项。

FrameLayout 是工具栏的一项。

我希望它对你有帮助。

于 2015-11-06T15:23:09.727 回答