Android工具栏仅在AppBarLayout崩溃时显示标题和副标题

use*_* rd 5 android android-toolbar android-collapsingtoolbarlayout android-appbarlayout

我有AppBarLayout,CollapsingToolbarLayout和工具栏活动。从代码设置标题和字幕。最初,我希望工具栏隐藏并在Appbar布局折叠时显示,使用我的代码可以正常工作(最初隐藏工具栏),但始终显示工具栏标题和副标题。仅在应用栏布局完全折叠时如何显示标题

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:titleEnabled="false"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

设置标题和字幕

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle("Title");
    getSupportActionBar().setSubtitle("sutitle");
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

use*_* rd 0

我使用xml 中的ControllableAppBarLayout解决了我的问题,并使用以下方法处理其 EXPAND、COLLAPSED、IDEAL 事件来显示/设置我的 TITLE 和 SUBTITLE。

 ControllableAppBarLayout appBarLayout = (ControllableAppBarLayout) findViewById(R.id.app_bar);
    appBarLayout.setOnStateChangeListener(new ControllableAppBarLayout.OnStateChangeListener() {

        @Override
        public void onStateChange(ControllableAppBarLayout.State toolbarChange) {
            switch (toolbarChange) {

                case COLLAPSED: {
                    Log.i(TAG, "COLLAPSED2");
                    if (mProfileDetails != null) {
                        getSupportActionBar().setTitle(mProfileDetails.userDetails.userFullname);
                        getSupportActionBar().setSubtitle(Html.fromHtml("<small>" + mProfileDetails.userDetails.headline + "</small>"));
                    }
                    break;
                }
                case EXPANDED:
                    Log.i(TAG, "EXPANDED");
                    getSupportActionBar().setTitle("");
                    getSupportActionBar().setSubtitle("");
                    break;

                case IDLE: // Just fired once between switching states
                    Log.i(TAG, "IDLE");
                    getSupportActionBar().setTitle("");
                    getSupportActionBar().setSubtitle("");
                    break;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)