Toolbar Support 包 24 以上边距留白问题

使用 Toolbar 的时候碰到个问题,借此分享给踩坑的童鞋们。
大家可能已经知道这个属性,用来解决Toolbar 左边留白问题

<item name="contentInsetStart">0dpitem>

但是本文的重点不在此,请往下看。


设置 Toolbar 返回按钮

actionBar.setDisplayHomeAsUpEnabled(true);

support 包版本为 23 及以下时

返回按钮与后面的 content 之间紧贴着,不存在边距,即使不设置

<item name="contentInsetStart">0dpitem>

Toolbar Support 包 24 以上边距留白问题_第1张图片

support 包版本为 24 及以上时

即使设置

<item name="contentInsetStart">0dpitem>

返回按钮与后面的 content 之间仍存在边距,如下图

Toolbar Support 包 24 以上边距留白问题_第2张图片

求解
通过对比 support 23 与 24 两个版本 Toolbar 默认属性的源代码可以发现问题所在,请看下面两张对比图
1. support 23 的源代码
Toolbar Support 包 24 以上边距留白问题_第3张图片
2. support 24 的源代码
Toolbar Support 包 24 以上边距留白问题_第4张图片

解决办法
给 Toolbar 设置以下属性

<item name="contentInsetStartWithNavigation">0dpitem>

效果如下图

Toolbar Support 包 24 以上边距留白问题_第5张图片

你可能感兴趣的:(Android)