Android 工具栏 TextView match_parent 变得比屏幕大

Android Toolbar TextView match_parent getting bigger than screen

我有一个带有 TextView 的工具栏,其中 match_parent 使文本居中,但每次我这样做时它都会变得比屏幕大并隐藏文本。

这里有截图运行

预览中的样子

最后是我是如何实现的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:theme="@style/ToolBarStyle" >

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="@string/title_activity_anuncios" />

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

    </android.support.design.widget.AppBarLayout>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

将其放入您的工具栏

app:contentInsetStart="0dp"

检查您的主题样式或从清单

中设置 activity 主题
 android:theme="@android:style/Theme.Translucent.NoTitleBar" 

如果 activity 主题正确,那么您可以检查 activity >> onCreate 方法。

   ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setTitle("Your Title");
    }