Toolbar的Title与NavigationIcon距离异常

来源:互联网 发布:如何恢复电脑网络设置 编辑:程序博客网 时间:2024/05/19 19:56

问题描述

当我将Support包由V22.2.0升级到V24.0.0的版本之后,发现原来正常显示的Toolbar显示异常。前提是我并没有修改任何代码。请看下图

NavigationIcon和Title的距离正确

距离显示正确.jpg
NavigationIcon和Title的距离出现了异常

距离显示异常.jpg

问题的解决方法

解决办法很简单,见代码
为了方便起见,先定义一个Toolbar的Theme

<style name="NoSpaceActionBarTheme" parent="Base.Widget.AppCompat.Toolbar">    <item name="contentInsetStart">0dp</item>    <item name="contentInsetStartWithNavigation">0dp</item></style>

如果在布局文件中添加Toolbar的话可以通过增加style来实现,代码如下

<android.support.v7.widget.Toolbar            style="@style/NoSpaceActionBarTheme"            android:id="@+id/toolbar"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize"            android:background="?attr/colorPrimary"            app:navigationIcon="?attr/homeAsUpIndicator"            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

如果定义过Activity的Theme是ActionBar的话,可以在Theme的定义中加上一句代码,如下

<style name="ActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">    <item name="colorPrimary">@color/colorPrimary</item>    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>    <item name="colorAccent">@color/colorAccent</item>    <item name="toolbarStyle">@style/NoSpaceActionBarTheme</item></style>

问题原因

为了搞明白为什么Support包从V22.2.0版本升级到V24.0.0就会出现这样的问题,还是需要翻看Toolbar的源代码,OK,我们直接看Toolbar的代码(V24.0.0包中),顺便说一下Toolbar是在appcompact-v7包下面

//用到的主要属性名称为contentInsetStartprivate final RtlSpacingHelper mContentInsets = new RtlSpacingHelper();//对应的属性名称为contentInsetStartWithNavigationprivate int mContentInsetStartWithNavigation;

mContentInsets 这个成员变量和mContentInsetStartWithNavigation用来控制NavigationIcon和Title之间的距离的,我们接着看这两个变量是如何影响这个距离的

最主要的代码在Toolbar中的onLayout方法中,下面我摘取主要代码来说明

final int paddingLeft = getPaddingLeft();//首先是获取系统的偏移量    int left = paddingLeft;//这段代码用来计算Navigation的Layoutif (shouldLayout(mNavButtonView)) {        if (isRtl) {            right = layoutChildRight(mNavButtonView, right, collapsingMargins,                    alignmentHeight);        } else {            //计算完之后left的距离为paddingLeft+mNavButtonView的宽度+mNavButtonView自身的偏移量            left = layoutChildLeft(mNavButtonView, left, collapsingMargins,                    alignmentHeight);        }    }//核心的方法,返回就是那个让距离错误的值final int contentInsetLeft = getCurrentContentInsetLeft();//left会从之前的left值也就是计算过Navigation的距离之后 和contentInsetLeft比较,取最大值left = Math.max(left, contentInsetLeft);...接下来计算Title的布局的时候左边距就是用的这个left

我们来看看getCurrentContentInsetLeft()这个方法

public int getCurrentContentInsetLeft() {    return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL            ? getCurrentContentInsetEnd()            : getCurrentContentInsetStart();}

因为我们是从左向右显示所以会调用getCurrentContentInsetStart()这个方法,我们继续看这个方法

public int getCurrentContentInsetStart() {    return getNavigationIcon() != null            ? Math.max(getContentInsetStart(), Math.max(mContentInsetStartWithNavigation, 0))            : getContentInsetStart();}

首先我们是有NavigationIcon的所以会走这个分支

Math.max(getContentInsetStart(), Math.max(mContentInsetStartWithNavigation, 0))

其中Math.max(mContentInsetStartWithNavigation, 0)返回的就是mContentInsetStartWithNavigation这个值
mContentInsetStartWithNavigation这个值就是从contentInsetStartWithNavigation这个属性中取得的
getContentInsetStart()这个返回的值就是contentInsetStart这个属性对应的值

所以最后就是比较contentInsetStart和contentInsetStartWithNavigation这两个属性的值


OK,接下来我们来看这两个属性的值在V22.2.0和V24.0.0的版本中到底是多少
具体的文件为首先找到对应版本的appcompact-v7包的aar文件
然后解压找到/res/values/values.xml这个文件

首先说明默认Toolbar的Style是Widget.AppCompat.Toolbar
Widget.AppCompat.Toolbar的Parent是Base.Widget.AppCompat.Toolbar
所以只要找到Base.Widget.AppCompat.Toolbar对应的Style就OK了

首先我们来看V22.2.0版本中,我找到了描述Toolbar属性的这段内容

<style name="Base.Widget.AppCompat.Toolbar" parent="android:Widget">    <item name="titleTextAppearance">@style/TextAppearance.Widget.AppCompat.Toolbar.Title</item>    <item name="subtitleTextAppearance">@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle</item>    <item name="android:minHeight">?attr/actionBarSize</item>    <item name="titleMargins">4dp</item>    <item name="maxButtonHeight">56dp</item>    <item name="collapseIcon">?attr/homeAsUpIndicator</item>    <item name="collapseContentDescription">@string/abc_toolbar_collapse_description</item>    <item name="contentInsetStart">16dp</item></style>

我们发现contentInsetStart这个是16dp,而没有contentInsetStartWithNavigation这个属性,这是因为contentInsetStartWithNavigation这个属性是在V22之后的版本才加上的,而V22的Toolbar代码中只会根据contentInsetStart来计算Title的左边距
接下来我们来看V24.0.0版本中的代码

<style name="Base.Widget.AppCompat.Toolbar" parent="android:Widget">    <item name="titleTextAppearance">@style/TextAppearance.Widget.AppCompat.Toolbar.Title</item>    <item name="subtitleTextAppearance">@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle</item>    <item name="android:minHeight">?attr/actionBarSize</item>    <item name="titleMargin">4dp</item>    <item name="maxButtonHeight">@dimen/abc_action_bar_default_height_material</item>    <item name="buttonGravity">top</item>    <item name="collapseIcon">?attr/homeAsUpIndicator</item>    <item name="collapseContentDescription">@string/abc_toolbar_collapse_description</item>    <item name="contentInsetStart">16dp</item>    <item name="contentInsetStartWithNavigation">@dimen/abc_action_bar_content_inset_with_nav</item>    <item name="android:paddingLeft">@dimen/abc_action_bar_default_padding_start_material</item>    <item name="android:paddingRight">@dimen/abc_action_bar_default_padding_end_material</item></style>

OK,contentInsetStart这个也是16dp,contentInsetStartWithNavigation这个定义在dimen中,我们来看看这个值

<dimen name="abc_action_bar_content_inset_with_nav">72dp</dimen>

OK,我们回过来看这段代码

//核心的方法,返回就是那个让距离错误的值final int contentInsetLeft = getCurrentContentInsetLeft();//left会从之前的left值也就是计算过Navigation的距离之后 和contentInsetLeft比较,取最大值left = Math.max(left, contentInsetLeft);

left的值一开始是NavigationIcon的宽度,一般为56dp,而contentInsetLeft这个值是72dp,所以left的值就变成了72dp,就最后导致了距离显示异常

至此,我们终于了解了这个错误的来龙去脉,不仅了解了怎么改,也了解了为什么这么改,同时了解了Toolbar的相关代码



原创粉丝点击