很早就遇到一个问题,如下图:

Toolbar去除标题与返回键间的间距_云阅

Toolbar去除标题与返回键间的间距_ToolBar_02

标题和左侧的返回键间距过大,之前在做​​CloudReader​​的时候就遇到了,一直没有解决,由于自身强迫症,心里始终和这个过不去,就研究了很久终于解决了。

之前在网上找了各种解决方法,比如给ToolBar设置

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"

不直接给ToolBar设置字体,在ToolBar里面包裹TextView,还是不行。

等等…

最后:

将​​mTitleToolBar.setNavigationIcon(R.drawable.icon_back);​​去掉后,发现标题顶格了,于是怀疑是NavigationIcon的问题。

将​​app:navigationIcon="@drawable/icon_back"​​设置在ToolBar属性上后可以直接看到效果,开始考虑是不是有一个属性是设置​​NavigationIcon​​与​​title​​之间的间距,果然发现了:

app:contentInsetStartWithNavigation="0dp"

附上完整代码:

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/title_tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorTheme"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_scrollFlags="enterAlways|scroll"
app:navigationIcon="@drawable/icon_back"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ToolbarStyle"
app:title="标题"
app:titleTextColor="@color/colorWhite">

后来直接搜了这个属性查了一下,发现之前还是有很多人遇到过,只不过我没检索出来,推荐一个分析的很好的: