CollapsingToolbarLayout setTitle() not working anymore

[亡魂溺海] 提交于 2019-12-09 00:30:18

问题


The setTitle() method from CollapsingToolbarLayout had some bugs already (like showing only after a scroll, fixed in v22.2.1).

Today I updated to v23.0.0, and it is simply not working, like no title is shown. By calling it multiple times with a delay, I can see that sometimes the title is there, but it is really not reliable (like, you switch to another fragment, then back to the first, and there's no title anymore).

I found there's a new attribute, app:titleEnabled or CollapsingToolbarLayout.setTitleEnabled(boolean). I have set both to true, but it doesn't change anything actually.

Is any of you experiencing the same behavior?

I wonder how many apps out there in the market are really using this Design Library, it has been full of bugs from the very first release and is not getting better.

Edit

With further testing, it seems (I'm not 100% sure) it is connected to the new AppBarLayout.setExpanded() api.

If I call:

collapsingToolbar.setTitle("title");

it works, but if I call

collapsingToolbar.setTitle("title");
appbarLayout.setExpanded(true, true);
//OR
appbarLayout.setExpanded(true, true);
collapsingToolbar.setTitle("title");

it doesn't - no title shown. Same with setExpanded(false, true), i. e. trying to collapse the toolbar.

Edit2 (sep 2015)

Well, no. The issue is present even when I never call abl.setExpanded() or app:expanded. Also, we're on v23.0.1 now and this still has not been fixed. Looks like only a few of us are experiencing it, because I couldn't find anything here nor in the official bug list. I filed a bug here.


回答1:


The title doesn't show because the layout height of the AppBarLayout is too small. I had a height of 110dp and this worked in v22.2.1. After I upgraded to v23.0.0, I had to change the height to at least 125dp in order for the title to show. No need to use the new attribute or method calls. Here is my layout:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_height="125dp"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:expandedTitleTextAppearance="@style/ExpandedAppBarTitle"
        app:expandedTitleMarginStart="14dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"/>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView android:id="@android:id/list"
                                        xmlns:android="http://schemas.android.com/apk/res/android"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/addbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    app:borderWidth="0dp"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_add" />




回答2:


I've tried some combinations in my app:

collapsingToolbar.setTitle("title");
appbarLayout.setExpanded(true, true); // works

collapsingToolbar.setTitle("title");
appbarLayout.setExpanded(true, false); // works

collapsingToolbar.setTitle("title");
appbarLayout.setExpanded(false, true); // works

collapsingToolbar.setTitle("title");
appbarLayout.setExpanded(false, false); // doesn't show title

I've called these methods in onCreateView() in my fragment. AppBarLayout is expanded by default, which can be changed via XML in your layout, although when I define it to not be expanded in XML, it doesn't show title as well. Weird stuff... anyway, if you'd have some idea how could I help further, let me know.



来源:https://stackoverflow.com/questions/32073812/collapsingtoolbarlayout-settitle-not-working-anymore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!