ToolBar的title的精确居中!

今天做一个项目:需要用到ToolBar的Title居中,

网上的流行的解决办法是在ToolBar里面嵌套一个TextView,布局如下:

<span style="font-size:18px;"><android.support.v7.widget.Toolbar
        android:id="@+id/recommand_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/colorWhite"
        android:layout_gravity="center">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/app_name"
            android:textSize="22sp" />

    </android.support.v7.widget.Toolbar></span>
最后的效果如下:

很明显:这样“壁纸精选”和“热门”的中轴线是不重合的!
那么有没有办法能使得它真正的居中呢?

方法很简单:只需要在ToolBar的属性设置中添加如下语句:

app:contentInsetStart="0dp"
最终的布局是这样的:
<android.support.v7.widget.Toolbar
        android:id="@+id/recommand_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/colorWhite"
        app:contentInsetStart="0dp"
        android:layout_gravity="center">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/app_name"
            android:textSize="22sp" />

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

最终的效果是这样的:

我们看到:这样就准确的居中了。

心情瞬间舒畅了!有木有!!

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值