CollapsingToolbarLayout:setShadowLayer

CollapsingToolbarLayout: setShadowLayer

我有一个collapsingToolbarLayout,在折叠模式和展开模式下都显示了用户名。

展开后是这样的:

半塌的样子:

您可以看到,在展开模式下,白色文本与背景图片的搭配不是很好。

通常为了提高文本的清晰度,如果这是一个文本视图,我通常会设置 ShadowLayer: http://developer.android.com/reference/android/widget/TextView.html#setShadowLayer(float,%20float,%20float,%20int)

在这种情况下,因为它是collapsingToolbarLayout,而不是textview,所以似乎该方法无法在文本后面设置阴影。

有谁知道可以增加 collapsingToolbarLayout 中文本清晰度的方法,例如在 collapsingToolbarLayout 中抚摸文本或添加阴影?

我通过查看此线程了解了这一点:

Android CollapsingToolbarLayout Title background

用户实际上问了一个非常相似的问题,答案是使用效果非常好的文本保护稀松布。

为了方便起见,我复制并粘贴了代码:

使用文本保护稀松布(向下滚动一点)。我的示例假定标题文本为白色,因此可能需要进行一些调整以针对您的情况进行优化。

在 CollapsingToolbarLayout 中,在 ivBigImage 之后添加以下内容:

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/sheet_text_scrim_height_top"
    android:background="@drawable/scrim_top"
    app:layout_collapseMode="pin"/>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/sheet_text_scrim_height_bottom"
    android:layout_gravity="bottom"
    android:layout_alignBottom="@+id/image"
    android:background="@drawable/scrim_bottom"/>

在您的 Drawable 文件夹中,添加:

scrim_top.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:angle="270"
    android:startColor="@color/translucent_scrim_top"
    android:centerColor="@color/translucent_scrim_top_center"
    android:endColor="@android:color/transparent"/>
</shape>

和scrim_bottom.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:angle="90"
    android:startColor="@color/translucent_scrim_bottom"
    android:centerColor="@color/translucent_scrim_bottom_center"
    android:endColor="@android:color/transparent"/>
</shape>

对于颜色,我使用了:

<color name="translucent_scrim_top">#26000000</color>
<color name="translucent_scrim_top_center">#0C000000</color>
<color name="translucent_scrim_bottom">#2A000000</color>
<color name="translucent_scrim_bottom_center">#0D000000</color>

对于尺寸,我使用了 88dp 的高度。

下面是应用代码后的效果: