Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

android - CollapsingToolbarLayout setTitle() does not update unless collapsed

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) When the CollapsingToolbarLayout does not have a title yet

2) At the moment the CollapsingToolbarLayout becomes fully collapsed

3) At the moment the CollapsingToolbarLayout starts to expand

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.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

EDIT: 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.

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.

This is what I have

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);
}

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>

Happy Coding.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...