简体   繁体   中英

Correct way to change Collapseable Toolbar title

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

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.

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.

EDIT: 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());
        }

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

Define you style, such as:

<style name="CollapsedAppBarTopic" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">20sp</item>
    <item name="android:textStyle">normal</item>
</style>

Then in you xml:

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/cardview_collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    android:fitsSystemWindows="false"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginEnd="48dp"
    app:expandedTitleMarginStart="16dp"
    app:expandedTitleMarginBottom="16dp"
    app:expandedTitleTextAppearance="@style/CollapsedAppBarTopic"
    >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM