Android Material Design - 折叠CollapsingToolbarLayout后如何更改工具栏的背景颜色

问题描述 投票:36回答:6

用户向下滚动屏幕后,CollapsingToolbarLayout中的图像将消失,并留下带有后退按钮,内容标题和设置菜单的工具栏。我想知道如何更改该工具栏处于“折叠”状态时的背景颜色。

我所指的动作类似于工具栏背景颜色变为绿色的动作:

在CollapsingToolbarLayout下面,我有一个带有CardViews的NestedScrollView

android material-design android-collapsingtoolbarlayout
6个回答
78
投票

我想你是在追求app:contentScrim

<android.support.design.widget.CollapsingToolbarLayout
    ...
    app:contentScrim="?attr/colorPrimary">
    <!-- Toolbar and ImageView here -->
</android.support.design.widget.CollapsingToolbarLayout>

12
投票

首先删除

app:contentScrim="?attr/colorPrimary"> 

来自CollapsingToolbarLayout

添加库

compile 'com.android.support:palette-v7:23.2.1'

并在java代码中添加以下代码

    Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ny);
    Palette.generateAsync(bitmap,
            new Palette.PaletteAsyncListener() {
                @Override
                public void onGenerated(Palette palette) {
                    Palette.Swatch vibrant =
                            palette.getVibrantSwatch();
                    int mutedColor = palette.getVibrantSwatch().getRgb();
                    if (vibrant != null) {
                        // If we have a vibrant color
                        // update the title TextView
                        collapseToolbar.setBackgroundColor(mutedColor);
                        //  mutedColor = palette.getMutedColor(R.attr.colorPrimary);
                        collapseToolbar.setStatusBarScrimColor(palette.getDarkMutedColor(mutedColor));
                        collapseToolbar.setContentScrimColor(palette.getMutedColor(mutedColor));

                    }
                }
            });

5
投票

只需使用CollapsingToolbarLayout XML属性contentScrimToolbar模式下设置collapsed背景颜色。

app:contentScrim="YOUR_TOOLBAR_COLOR"

这是一个例子:

<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:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:id="@+id/img_group_photo"
        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/anim_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>

希望这会有所帮助〜


1
投票

也许你一直在寻找的是:

myCollapsingToolbar.setContentScrimColor(getResources().getColor(R.color.my_color_id));

它对我有用,并且在折叠后更改了折叠工具栏的颜色,以帮助我适应折叠工具栏满刻度时显示的图像的主要颜色。有了这个,显然可以以编程方式改变颜色!

我知道我迟到了,但我希望它能有所帮助。


0
投票

您可以使用AppBarLayout的偏移侦听器,并根据所需行为更改CollapsingTollbar属性。

appBarLayout.addOnOffsetChangedListener { _, verticalOffSet ->
            if (Math.abs(verticalOffSet) == appBarLayout.totalScrollRange) {
                //Collapsed
                toolBar.setBackgroundDrawable(ContextCompat.getDrawable(this,
                        R.drawable.last_revolut_gradient))
            } else {
                //Expanded
                toolBar.setBackgroundColor(ContextCompat.getColor(this,
                        android.R.color.transparent))
            }
        }

-1
投票
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.header);

    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
        @SuppressWarnings("ResourceType")

        @Override
        public void onGenerated(Palette palette) {
            Palette.Swatch vibrant =
                    palette.getVibrantSwatch();

            if (vibrant != null) {

                collapsingToolbar.setBackgroundColor(getResources().getColor(R.color.cpb_blue));
                collapsingToolbar.setStatusBarScrimColor(getResources().getColor(R.color.cpb_blue));
                collapsingToolbar.setContentScrimColor(getResources().getColor(R.color.cpb_blue));

            }
        }

    });
© www.soinside.com 2019 - 2024. All rights reserved.