更改可折叠工具栏标题的正确方法 [英] Correct way to change Collapseable Toolbar title

查看:71
本文介绍了更改可折叠工具栏标题的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我更改collapsesingToolbar的标题时,标题不变.

When I change title of collapsed collapsingToolbar, the title doesn't change.

我尝试了getSupportActionBar.setTitle和crashToolbar.setTitle,但这没有帮助.告诉我,怎么了?

I've tried getSupportActionBar.setTitle and collapseToolbar.setTitle, but it didn't help. Tell me, what's the problem ?

推荐答案

我相信

I believe that this issue describes what you are experiencing. I also had this issue and solved it today. Essentially the code that handles the collapsing text only update the text when the current text is null or the size of the text changes. Currently this is a bug that is closed and the patch is scheduled for a future release of the design library. For now use my workaround of just changing the size of the text and then changing it back.

这就是我所拥有的

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>

快乐编码.

以下代码来自collapsingtext帮助器,该帮助器在可折叠工具栏布局内用于控制该视图的文本.

The below code is from the collapsingtext helper that is used inside the collapsing toolbar layout to control the text of that view.

        if(availableWidth > 0.0F) {
            updateDrawText = this.mCurrentTextSize != newTextSize;
            this.mCurrentTextSize = newTextSize;
        }

        if(this.mTextToDraw == null || updateDrawText) {
            this.mTextPaint.setTextSize(this.mCurrentTextSize);
            CharSequence title = TextUtils.ellipsize(this.mText, this.mTextPaint, availableWidth, TruncateAt.END);
            if(this.mTextToDraw == null || !this.mTextToDraw.equals(title)) {
                this.mTextToDraw = title;
            }

            this.mTextWidth = this.mTextPaint.measureText(this.mTextToDraw, 0, this.mTextToDraw.length());
        }

令人讨厌的行是updateDrawText = this.mCurrentTextSize != newTextSize;,它设置布尔值来确定是否在此行中更改文本.if(this.mTextToDraw == null || updateDrawText) {因此,当折叠的工具栏布局重新计算其视图时,确定文本大小的决定因素是textsize.如果您没有更改文字大小,那么折叠的工具栏布局标题将不会发生变化,直到它被折叠或从折叠位置展开为止.

the offending lines are updateDrawText = this.mCurrentTextSize != newTextSize; which sets the boolean to determine if we change the text in this line if(this.mTextToDraw == null || updateDrawText) { So when the collapsing toolbar layout recalculates its view the determining factor to set the text is the textsize. If you did not change the text size then your collapsing toolbar layout title will not change untill it is collapsed or expanded from collapsed position

这篇关于更改可折叠工具栏标题的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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