安卓:CollapsingToolbarLayout中心扩展的文本,但没有崩溃文本 [英] Android: CollapsingToolbarLayout centers expanded text but not collapsed text
问题描述
我有一个 CollapsingToolbarLayout
被定义无论是在崩溃和扩展模式,以中心:
I have a CollapsingToolbarLayout
that is defined to center both in collapsed and in expanded modes:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="286dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedTitleGravity="center"
app:expandedTitleGravity="center"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
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:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|exitUntilCollapsed"/>
<ImageView
android:id="@+id/someIcon"
android:layout_width="56dp"
android:layout_height="wrap_content"
android:src="@drawable/some_icon"
android:padding="16dp"
android:layout_gravity="top|end"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
我定义工具栏标题,但是当我崩溃它,我可以看到标题在对角线方向移动,而不是直线上升,它本身略有对齐到工具栏线的中心的权利。请注意,宽度为match_parent和倒塌的引力中心,那么为什么会这样发生,我应该怎么解决?
I define a title for the toolbar but when I collapse it I can see the title moving in a diagonal direction instead of straight up and it aligns itself slightly to the right of the center of the toolbar line. Notice that the width is match_parent and the collapsed gravity is center, so why could this be happening and how should I fix it?
•第一张截图:是什么样子,如果我使用崩溃 - >中心和扩展 - >中心比重然后塌陷布局。注意,它是在屏幕的中心的右侧。
• First screenshot: what it looks like if I use collapse -> center and expanded -> center gravities and then collapse the layout. Notice that it's to the right of the center of the screen.
•第二个屏幕:是什么样子,如果我摆脱崩溃 - >重心,但保留扩展 - >重心,然后折叠布局。请注意,它的排列在默认情况下离开了。
• Second screenshot: what it looks like if I get rid of collapse -> center gravity but leave expanded -> center gravity and then collapse the layout. Notice that it's aligned left by default.
•第三截图:它是什么样子扩大
我到目前为止已经试过的东西,以解决这个(没有成功):
Things that I've tried so far to solve this (without success):
•摆脱collapsedGravity的,只留下expandedGravity
• Getting rid of the collapsedGravity and leaving only the expandedGravity
•使用默认的Roboto字体为标题
• Using the default roboto font for the title
•这两个工具栏设置填充和利润为0,并且倒塌布局
• Setting padding and margins to 0 for both the toolbar and the collapsing layout
•设定重心CENTER_HORIZONTAL,而不是中心
• setting gravity center_horizontal instead of center
我发现的唯一的解决方法是正确的,使这项工作是使用一个单独的TextView举行的称号,而不是设置的标题collapsingtoolbarlayout的(这使得正确的称号崩溃的中心)。这是不是最佳的,所以我倒是AP preciate知道,如果在CPL中有一个错误,或者有使用默认的标题做同样的事情的一种方式。
The only workaround I've found that makes this work properly is to use a separate textview to hold the title instead of setting the title for the collapsingtoolbarlayout (this makes the title collapse properly to the center). This isn't optimal so I'd appreciate knowing if the CPL has a bug in it or if there's a way to use the default title to do the same thing.
推荐答案
您需要设置应用:expandedTitleGravity =中心
并删除应用:collapsedTitleGravity =中心
从code像
You need to set app:expandedTitleGravity="center"
and remove app:collapsedTitleGravity="center"
from your code like
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:expandedTitleGravity="center"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
希望这有助于你
这篇关于安卓:CollapsingToolbarLayout中心扩展的文本,但没有崩溃文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!