去除ToolBar的左边距

Material design出来有些时日,近日项目改版想尝试下新控件。使用ToolBar过程中发现标题无法居中,于是在toolBar标签中引入一个布局显示标题。

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    >
                    <TextView
                        android:id="@+id/tv_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="全部门店"
                        android:textColor="@color/white"
                        android:textSize="18sp"/>
                </LinearLayout>
            </android.support.v7.widget.Toolbar>

现在看起来可以居中了,有发现了新的问题。如下图,明显发现toobar有左边距

这里写图片描述

两种方法解决:

  • 直接在toolBar中添加设置
     app:contentInsetStart="0dp"
  • 复写tooBar的style,在AppTheme中引用。这种方法一劳永逸,无需对每个toolBar设置
 <style name="CustomToolBar" parent="Widget.AppCompat.Toolbar">
        <item name="contentInsetStart">0dp</item>
    </style>

在AppTheme中引用

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">@color/colorPrimaryDark</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="toolbarStyle">@style/CustomToolBar</item>
    </style>

这里写图片描述
酱紫就去除了toolBar的左边距,并且居中标题啦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值