盒子
盒子

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

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

间距过大

间距正常

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

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

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

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

最后:

1
mTitleToolBar.setNavigationIcon(R.drawable.icon_back);

去掉后,发现标题顶格了,于是怀疑是NavigationIcon的问题。将

1
app:navigationIcon="@drawable/icon_back"

设置在ToolBar属性上后可以直接看到效果,开始考虑是不是有一个属性是设置NavigationIcon与title之间的间距,果然发现了:

1
app:contentInsetStartWithNavigation="0dp"

附上完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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">


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

支持一下
扫一扫,支持我
  • 微信扫一扫
  • 支付宝扫一扫