最初折叠ActionBarLayout时,CollapsingToolbarLayout标题会消失

时间:2015-10-02 11:38:36

标签: java android drawerlayout android-collapsingtoolbarlayout

当应用向下滚动并且应用栏折叠时,标题会以某种方式消失,即使在展开应用栏后,有时也不会显示。

这是我的问题的 gif 动画: 的 animation.gif

这是我的xml:

activity_main.xml中:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar.main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

            <ImageView
                android:src="@drawable/book1"
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="240dp"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.5"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"/>

            <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"
                app:layout_scrollFlags="scroll|enterAlways"/>


        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

我甚至以编程方式设置标题:

CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
collapsingToolbar.setTitle(getString(R.string.app_name));
collapsingToolbar.setTitleEnabled(true);

2 个答案:

答案 0 :(得分:3)

众所周知, Bug 描述了here。 Chris Banes已完成此问题的解决方法,预计将在未来版本的设计支持库中修复

根据评论#7,临时解决方法如下:

不要让CollapsingToolbarLayout处理您的标题,而是添加TextView。 然后,您可以通过向包含CollapsingToolbarLayout的AppBarLayout添加OnOffsetChangedListener来动画(缩放/翻译)此自定义标题视图。此侦听器为您提供偏移量,您可以使用该偏移量计算源与目标高度的关系,从而计算新的文本大小:

float scaledTextSize = collapsedTextSize + ((expandedTextSize - sourceTextSize) * ((float) Math.abs(offset) / appBarLayout.getTotalScrollRange()));

文本大小可以手动设置,也可以通过您使用的TextAppearance样式设置。

<强> 被修改 我确认此问题现已在最新的支持库版本 v23.1.0

中修复

答案 1 :(得分:0)

删除app:contentScrim="?attr/colorPrimary"