Android Toolbar TextView match_parent 变得比屏幕大

Posted

技术标签:

【中文标题】Android Toolbar TextView match_parent 变得比屏幕大【英文标题】:Android Toolbar TextView match_parent getting bigger than screen 【发布时间】:2017-12-29 19:15:51 【问题描述】:

我有一个带有 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_
    android:layout_
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_
        android:layout_
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp">

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

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_
                android:layout_
                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_
        android:layout_>

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

【问题讨论】:

【参考方案1】:

检查您的主题样式或从清单中设置活动主题

 android:theme="@android:style/Theme.Translucent.NoTitleBar" 

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

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

【讨论】:

谢谢,我完全忘记了【参考方案2】:

把它放在你的工具栏中

app:contentInsetStart="0dp"

【讨论】:

以上是关于Android Toolbar TextView match_parent 变得比屏幕大的主要内容,如果未能解决你的问题,请参考以下文章

Android Toolbar 标题居中及字体样式自定义

Android ToolBar标题文字居中的方法

android.widget.Toolbar 无法转换为 androidx.appcompat.widget.Toolbar(android 工具栏)

Android ToolBar使用

android 两种方式设置toolbar

Android Toolbar使用及Fragment中的Toolbar处理