工具栏标题不居中 [英] ToolBar title doesn't center

查看:45
本文介绍了工具栏标题不居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将工具栏标题居中:

我该怎么做?

解决方案

我遇到了类似的问题,不记得在哪里找到答案了,但工具栏默认有 16dp 的内容插入.您需要将其删除才能使徽标居中.

app:contentInsetStart="0dp"

确保使用 app 命名空间.

因此您的工具栏布局将如下所示:

 <文本视图android:id="@+id/toolbar_title"android:layout_width="match_parent"android:layout_height="match_parent"机器人:重力=中心"机器人:文本=标志"android:textColor="@color/colorAccent"android:textSize="32sp"android:textStyle="粗体"工具:忽略=硬编码文本"/></android.support.v7.widget.Toolbar>

更新

contentInsetStart 设置为0dp"也对我不起作用.所以我尝试给 TextView 提供负填充(-48dp)并且它起作用了.

<文本视图android:id="@+id/toolbar_title"android:layout_width="match_parent"android:layout_height="match_parent"机器人:重力=中心"机器人:文本=标志"android:textColor="@color/colorAccent"android:textSize="32sp"android:textStyle="粗体"工具:忽略=硬编码文本"android:paddingStart="-48dp"android:paddingLeft="-48dp"/></android.support.v7.widget.Toolbar>

I am trying to center my ToolBar title:

<android.support.design.widget.AppBarLayout
    android:id="@+id/top_app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="LOGO"
            android:textColor="@color/colorAccent"
            android:textSize="32sp"
            android:textStyle="bold"
            tools:ignore="HardcodedText" />

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

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

ToolBar aligns it's title left by default. To align the title center I have added a TextView inside the tool bar. But it doesn't center exactly. I have try to give negative margin from left but it didn't work.

How can I do it?

解决方案

I had a similar problem, can't remember where I found the answer but Toolbar has a 16dp content inset by default. You need to remove that to center the logo.

app:contentInsetStart="0dp"

Make sure you use the app name space.

So your Toolbar layout would look like this:

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:paddingLeft="0dp"
            android:paddingRight="0dp"
            android:layout_width="match_parent"
            app:contentInsetStart="0dp"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary">

            <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="LOGO"
                    android:textColor="@color/colorAccent"
                    android:textSize="32sp"
                    android:textStyle="bold"
                    tools:ignore="HardcodedText" />

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

UPDATE

Setting contentInsetStart to "0dp" also didn't work for me. So I tried giving negative padding (-48dp) to TextView and it worked.

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

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="LOGO"
        android:textColor="@color/colorAccent"
        android:textSize="32sp"
        android:textStyle="bold"
        tools:ignore="HardcodedText"
        android:paddingStart="-48dp"
        android:paddingLeft="-48dp"/>

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

这篇关于工具栏标题不居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆