CollapsingToolbarLayout setTitle() not working anymore

人走茶凉 提交于 2019-11-30 17:46:54

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" />

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.

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