Toolbar 自定义布局去除左右两边间距

v7 支持包中的 Toolbar,既可以直接代替 actionBar 使用,也可以嵌套布局自定义actionBar 样式,但是在嵌套布局自定义样式的时候,左右两边总会有一定的间距。直接在 toolbar 中添加布局:

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

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_red_dark" />

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

toolbar 背景为蓝色,自定义布局背景为红色,默认是这样的:

网上查找去除左边间距的方法都说在布局中设置 toolbar 一个属性就可以了,如下:

app:contentInsetStart=”0dp”

但是加上后的效果是这样的:

只不过是间距变窄了一些。经过测试,发现还要设置 toolbar 的 paddingLeft 和 paddingRight 值为 0,当然,直接设置 padding 为 0 也是可以的。加上之后终于没有间距了:

如果在代码里设置了显示返回箭头,即

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

,则需要给布局添加属性设置:

app:contentInsetStartWithNavigation=”0dp”

设置前:
这里写图片描述
设置后:
这里写图片描述
可以看到,返回箭头占的位置变窄了。
最后,完整布局是这样的:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/holo_blue_bright"
        android:padding="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_red_dark" />

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

PS:使用 toolbar 代替 actionBar 时别忘了设置 Activity 主题为 NoActionBar ~~~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值