[英]CollapsingToolbarLayout title disappears when ActionBarLayout is initially collapsed
When the app scroll down and app bar collapsed, the title disappear somehow and sometimes don't show again even after expanding the app bar. 当应用程序向下滚动并且应用程序栏折叠时,标题会以某种方式消失,有时甚至在展开应用栏后也不会再次显示。
here's a gif animation of my problem: animation.gif 这是我的问题的gif动画: animation.gif
here's my xml: 这是我的xml:
activity_main.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: 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>
I even set title programmatically: 我甚至以编程方式设置标题:
CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
collapsingToolbar.setTitle(getString(R.string.app_name));
collapsingToolbar.setTitleEnabled(true);
This is well known Bug described here . 这是很好的描述已知的bug 在这里 。 Chris Banes already done with workaround for this issue and its expected to be fixed in Future Release of Design Support Library Chris Banes已经完成了针对此问题的解决方法,并且预计将在未来版本的设计支持库中修复
As per comment #7, Temporary Workaround could be as below : 根据评论#7,临时解决方法如下:
Don't let the CollapsingToolbarLayout
handle your title but add a TextView to it. 不要让CollapsingToolbarLayout
处理您的标题,而是向其添加TextView。 Then, you can animate (scaling / translating) this custom title view by adding an OnOffsetChangedListener to the AppBarLayout containing the CollapsingToolbarLayout. 然后,您可以通过向包含CollapsingToolbarLayout的AppBarLayout添加OnOffsetChangedListener来动画(缩放/翻译)此自定义标题视图。 This listener gives you the offset with which you can compute the relation of source to target height and therefore the new text size: 此侦听器为您提供偏移量,您可以使用该偏移量计算源与目标高度的关系,从而计算新的文本大小:
float scaledTextSize = collapsedTextSize + ((expandedTextSize - sourceTextSize) * ((float) Math.abs(offset) / appBarLayout.getTotalScrollRange()));
The text sizes can be either set manually or through the TextAppearance styles you use. 文本大小可以手动设置,也可以通过您使用的TextAppearance样式设置。
Edited I confirmed that this issue is being fixed now in latest support library version v23.1.0 编辑我确认此问题现在已在最新的支持库版本v23.1.0中得到修复
删除app:contentScrim="?attr/colorPrimary"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.