使用 Toolbar 的時候碰到個問題,借此分享給踩坑的童鞋們。
大家可能已經知道這個屬性,用來解決Toolbar 左邊留白問題
<item name="contentInsetStart">0dp</item>
但是本文的重點不在此,請往下看。
設置 Toolbar 返回按鈕
actionBar.setDisplayHomeAsUpEnabled(true);
support 包版本為 23 及以下時
返回按鈕與后面的 content 之間緊貼著,不存在邊距,即使不設置
<item name="contentInsetStart">0dp</item>
support-23.png
support 包版本為 24 及以上時
即使設置
<item name="contentInsetStart">0dp</item>
返回按鈕與后面的 content 之間仍存在邊距,如下圖
support-24.png
求解
通過對比 support 23 與 24 兩個版本 Toolbar 默認屬性的源代碼可以發現問題所在,請看下面兩張對比圖
-
support 23 的源代碼
support23.png -
support 24 的源代碼
support24.png
解決辦法
給 Toolbar 設置以下屬性
<item name="contentInsetStartWithNavigation">0dp</item>
效果如下圖
support-24-0.png