【问题标题】:Android CollapsingToolbarLayout Title backgroundAndroid CollapsingToolbarLayout 标题背景
【发布时间】:2015-10-11 03:11:26
【问题描述】:

我正在使用新的 Android 设计支持库中的 CollapsingToolbarLayout。

我已经设置了它的标题并且它工作正常,我唯一的问题是当你滚动时,文本会丢失,这取决于背景中的图像。

我想做的是为 CollapsingToolbarLayout 标题设置背景,但我还没有找到方法。

有没有办法做到这一点?

布局:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/ivBigImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax"/>

        <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/ThemeOverlay.AppCompat.Light"/>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_gravity="fill_vertical"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="24dp">

        <android.support.v7.widget.CardView
            android:id="@+id/cvDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">

            <LinearLayout
                style="@style/Image.Info.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/description"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

                <TextView
                    android:id="@+id/tvDescription"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text=""/>

            </LinearLayout>

        </android.support.v7.widget.CardView>


    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

在activity中设置CollapsingToolbarLayout标题:

CollapsingToolbarLayout collapsingToolbar =
            (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle("Some title here");

编辑:

当我折叠工具栏时,您可以在此处看到一系列图像。您可以看到标题文本是如何不可读的。问题是我无法控制我显示的图像,因此对于某些图像来说它看起来不错,但对于其他图像,比如这个例子,它看起来一点也不好看而且不可读。我想到的可能是给文本添加某种背景,所以文本后面总是有相同的颜色,并且总是可读的。

【问题讨论】:

  • 无需添加代码,只是简单的CoordinatorToolbarLayout。这里没什么特别的 - 但无论如何我都会这样做
  • @Jared Burrows 这里不需要代码,这是基本功能。另外,为什么要麻烦发表评论?如果您不想提供帮助,请关闭标签并继续您的生活。
  • 嗨@roy_lennon 我一直在处理与您完全相同的问题,但是我已经应用了下面描述的解决方案而没有任何后果,我仍然像以前一样在 collapsingtoolbarlayout 中看到图像视图(并且与您在问题中所描述的完全一样)您能帮我吗?
  • @roy_lennon 您好,您如何将图像动态添加到 CollapsinToolbarLAyout。

标签: android android-styles android-collapsingtoolbarlayout android-support-design


【解决方案1】:

使用text protection scrim(向下滚动一点)。我的示例假设标题文本是白色的,因此可能需要进行一些调整以针对您的情况进行优化。

在您的 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 的高度。

【讨论】:

  • 谢谢,这似乎解决了我的问题。我必须玩一点并稍微调整一下,但我认为就是这样。谢谢!
  • 您好@Amagi82 我一直在尝试使用您的解决方案解决我在应用程序中遇到的这个问题,但它不起作用。第一个视图有效(按钮的上视图),如果您在 de xml 布局中使用 android studio 中的“设计”选项卡,但另一个视图底部视图不显示,并且在 xml 中的设计选项卡上出现在协调器布局之外的其他组件上方。你知道会发生什么吗?谢谢!
  • @neteot 如果没有看到您的代码并且没有看到正在发生的事情的图像,我不确定,但我要检查的第一件事是我在哪里以及以什么顺序将我的观点放在布局。 CollapsingToolbarLayout 里面有稀松布吗?它们的顺序是什么?它们是按顺序绘制的,因此如果您将完整尺寸的图像放在 FrameLayout(如 CollapsingToolbarLayout)内的另一个视图之后,它将绘制在它的顶部。让我知道这是否有帮助,或者请澄清我是否走在正确的轨道上。祝你好运。 :)
  • 我将android:fitsSystemWindows="true" 添加到我的View。否则它从状态栏下方开始,看起来很糟糕。
  • @Amgi82 我终于做到了!!经过两天的挣扎,我找到了解决方案……在成绩文件中,我使用的是支持库版本 22.+,它必须是 23.+ .....我快疯了!非常感谢!
【解决方案2】:

使用 Amgi82 示例中的文本保护稀松布并在 CollapsingToolbarLayout 上添加 app:expandedTitleTextAppearance 参数。

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    .
    app:expandedTitleTextAppearance="@style/TextAppearance.Design.CollapsingToolbar.Expanded.Shadow"
    .
    .
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

例如在你的样式 xml 中添加这个:

<style name="TextAppearance.Design.CollapsingToolbar.Expanded.Shadow">
    <item name="android:shadowDy">0</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowRadius">8</item>
    <item name="android:shadowColor">@android:color/black</item>
</style>

【讨论】:

  • 这是一个非常简单的解决方法!其他的我没试过
【解决方案3】:

编辑:

如果您想在工具栏“缩小”后更改其颜色,则需要将折叠工具栏布局的contentScrim 属性设置为该颜色:

<android.support.design.widget.CollapsingToolbarLayout
        app:contentScrim="@color/[color you want]"
        ...>

据我了解,将此属性的值指向您希望工具栏变成的颜色将解决您的问题。

希望能回答您的问题!

【讨论】:

  • 我已经有了,我只想让折叠工具栏标题的背景有另一种颜色
  • @roy_lennon 抱歉,完全误解了这个问题。查看编辑。
  • 谢谢!我已经知道了......但这不是我想要做的......我正在尝试在折叠工具栏布局标题折叠之前更改它......
  • 啊!!我知道了。试试这个: ((CollapsingToolbarLayout) collapsingToolbar).setExpandedTitleColor();
  • 这是您要找的吗?
【解决方案4】:

通过编写这么多代码,这里确实需要做很多工作。 我通过两种方式实现了这一点。

1 使用透明黑色视图的简单解决方法
代码>>

代码说明:
1 CollapsingToolbarLayout 的样式只有文本大小。
2 默认的 CollapsingToolbarLayout 底部边距被覆盖为 16dp。
3.我们的带有视差collapseMode 的标题是一个带有ImaveView 和一个View 的RelativeLayout。
4。这个简单的 View 在顶部标题的底部有一个 BG 具有对比颜色的相对布局(这里 #77000000),作为 CollapsingToolbarLayout 的白色折叠标题的 BG。

  <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/redeem_detail_collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/color_window_background"
        android:fitsSystemWindows="true"
        app:expandedTitleMarginBottom="16dp"
        app:expandedTitleTextAppearance="@style/CollapsingTitleStyle"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <!--header view-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_collapseMode="parallax">

            <ImageView
                android:id="@+id/redeem_detail_top_bg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/splash" />

            <!--A view that draws a semi tranparent black overlay so that title is visible once expanded -->
            <View
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_alignParentBottom="true"
                android:background="@color/black_transparent" />

        </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/redeem_detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:title="Redeem" />

    </android.support.design.widget.CollapsingToolbarLayout>

样式 1 的图片:

a) 当 CollapsingToolbarLayout 标题完全展开时:

b) 当 CollapsingToolbarLayout 标题在向上滚动时缩小时:

2回复above

Joao Ferreira 已经提到了方法。

以下是使用 shadowRadius=16 时的样子:注意阴影

如果有任何困惑,请更新或询问更多信息:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多