Toolbar的Title与NavigationIcon距离异常

问题描述

当我将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包下面

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

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

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

final int paddingLeft = getPaddingLeft();
//首先是获取系统的偏移量    
int left = paddingLeft;
//这段代码用来计算Navigation的Layout
if (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的相关代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,204评论 0 17
  • 本文出自 “阿敏其人” 简书博客,转载或引用请注明出处。 一、Google口中的ToolBar 从Toolbar说...
    阿敏其人阅读 4,143评论 2 36
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 黑暗中的四肢像是被空气中的悬浮颗粒托起来,变得越来越轻盈,化蝶了么?一颗坚强的心被嗔念痴念欲念层层包裹得严严实实,...
    禾叶兄弟阅读 227评论 0 1