当ActionBarLayout最初被折叠时,CollapsingToolbarLayout的标题会消失。

3
当应用程序向下滚动并折叠应用程序栏时,标题会以某种方式消失,并且有时即使在展开应用程序栏后也不会再次显示。
这是我的问题的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个回答

4

这是一个众所周知的 Bug在这里有描述。Chris Banes已经完成了对此问题的解决方法,并预计在未来发布的Design Support Library中进行修复。

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

不要让CollapsingToolbarLayout处理您的标题,而是向其中添加一个TextView。 然后,您可以通过将OnOffsetChangedListener添加到包含CollapsingToolbarLayout的AppBarLayout中来动画化(缩放/平移)此自定义标题视图。此侦听器为您提供偏移量,您可以借此计算源高度与目标高度之间的关系,从而得出新的文本大小:

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

文本大小可以手动设置,也可以通过使用的TextAppearance样式进行设置。 编辑 我确认这个问题现在已经在最新的支持库版本v23.1.0中得到修复。

这个问题现在已经在最新的设计支持库版本(v23.1.0)中得到解决。我也更新了我的答案。 - Bhavesh Patadiya
@BhaveshPatadiya 你确定它已经被修复了吗?我正在使用“compile 'com.android.support:design:23.0.1'”,但这个问题仍然存在。 - jensiepoo
@jensiepoo 是的,它将在“23.1.0”中得到解决,请尝试使用此版本。 - Bhavesh Patadiya
10
这个 bug 似乎又出现了,我在最新的版本中遇到了问题。 - Alan

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接