【问题标题】:CollapsingToolbarLayout title disappears when ActionBarLayout is initially collapsed当 ActionBarLayout 最初折叠时,CollapsingToolbarLayout 标题消失
【发布时间】:2015-10-02 11:38:36
【问题描述】:

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

这是我的问题的 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);

【问题讨论】:

    标签: java android drawerlayout android-collapsingtoolbarlayout


    【解决方案1】:

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

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

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

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

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

    已编辑 我确认此问题现已在最新的支持库版本 v23.1.0

    中得到修复

    【讨论】:

    • 此问题现已在最新设计支持库版本 (v23.1.0) 中得到修复。我现在也更新我的答案。
    • @BhaveshPatadiya 你确定它正在修复吗?我正在使用“编译'com.android.support:design:23.0.1'”,这个问题仍然存在
    • @jensiepoo 是的,它正在“23.1.0”中解决,尝试使用它。
    • 错误似乎又回来了,我发现最新版本存在问题
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 2015-09-24
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多