启用“后退按钮"时工具栏标题不在中心 [英] Toolbar title not in center when Back Button is enable

查看:70
本文介绍了启用“后退按钮"时工具栏标题不在中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在中心显示我的工具栏标题,并使用此答案中给出的方法:-工具栏中心标题

I'm trying to display my toolbar title in the center and to do it I use the method which is given in this answer :-Toolbar Center title

但是,当我通过以下代码在activity中启用后退按钮时:

However, when I enable back button in my activity by following code:

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);

工具栏的标题没有出现在中心位置,而是向右稍微偏心.

The title of toolbar doesn't show up in the center but slightly off-centered towards the right.

如何在不受后退按钮或菜单栏影响的情况下实现居中居中标题?

How can I achieve centered title without being affected by the back button or menu bar?

推荐答案

在工具栏内添加TextView&不要忘记在TextView中设置以下属性.

Add a TextView inside the Toolbar & don't forget to set the following attribute inside your TextView.

android:layout_marginRight =?android:attr/actionBarSize"



android:layout_marginEnd =?android:attr/actionBarSize"

android:layout_marginRight="?android:attr/actionBarSize"

OR

android:layout_marginEnd="?android:attr/actionBarSize"

代码段:

<android.support.v7.widget.Toolbar
    android:id="@+id/custom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@android:color/holo_red_dark">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="abc"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_marginRight="?android:attr/actionBarSize"
        android:gravity="center"/>

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

有关更多信息,请参考教程.

Refer to this tutorial for more information.

这篇关于启用“后退按钮"时工具栏标题不在中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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