CollapsingToolbarLayout setTitle() 除非折叠,否则不会更新 [英] CollapsingToolbarLayout setTitle() does not update unless collapsed

本文介绍了CollapsingToolbarLayout setTitle() 除非折叠,否则不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的设计库,我们应该在 CollapsingToolbarLayout 上设置工具栏标题,而不是 Toolbar 本身(至少在使用折叠工具栏时).但是setTitle()只在以下特定情况下更新标题:

With the new Design Library, we're supposed to set the toolbar title on the CollapsingToolbarLayout, not the Toolbar itself(at least when using the collapsing toolbar). But setTitle() only updates the title in the following specific circumstances:

  1. CollapsingToolbarLayout 还没有标题时

此时 CollapsingToolbarLayout 完全折叠

此时 CollapsingToolbarLayout 开始展开

我真正想做的是在完全展开时让标题变成 EditText,允许用户给他/她的角色一个名字,然后显示为标题.我试图通过调用来强迫这个问题invalidate()requestLayout(),以及 CollapsingToolbarLayout 子项上的这两种方法.没有效果.

What I'm actually trying to do is make the title become an EditText when fully expanded, allowing the user to give his/her character a name, which then displays as the title. I've tried to force the issue by calling invalidate() or requestLayout(), as well as both of those methods on CollapsingToolbarLayout's children. No effect.

推荐答案

不再需要此解决方案.在 v22.2.1

This solution is no longer needed. bug fixed in v22.2.1

我不想只留下链接,所以这里是完整的解决方案.

I didnt want to just leave links so here is the full solution.

出现该错误是因为处理可折叠标题的代码仅在当前标题为空或文本大小更改时才更新实际标题.解决方法是更改​​标题文本大小,然后再改回来.我使用了 0.5 sp,所以没有太大的跳跃.更改文本大小会强制更新文本并且没有闪烁.只是文字大小略有变化.

The bug occurs because the code to handle the collapsable title only updates the actual title if the current title is null or the text size has changed. The workaround is to change the title text size and then change it back. I used 0.5 sp so there was not too much of a jump. Changing the text size forces the text to be updated and there is no flicker. just a slight text size change.

这就是我所拥有的

private void setCollapsingToolbarLayoutTitle(String title) {
    mCollapsingToolbarLayout.setTitle(title);
    mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
    mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
    mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBarPlus1);
    mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBarPlus1);
}

在styles.xml中我有

in styles.xml I have

<style name="ExpandedAppBar" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">28sp</item>
    <item name="android:textColor">#000</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="CollapsedAppBar" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="ExpandedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">28.5sp</item>
    <item name="android:textColor">#000</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="CollapsedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">24.5sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textStyle">normal</item>
</style>

这篇关于CollapsingToolbarLayout setTitle() 除非折叠,否则不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆