Android材质设计-如何在折叠工具栏布局折叠后更改工具栏的背景色

Android材质设计-如何在折叠工具栏布局折叠后更改工具栏的背景色,android,material-design,android-collapsingtoolbarlayout,Android,Material Design,Android Collapsingtoolbarlayout,用户向下滚动屏幕后,折叠工具栏布局中的图像消失,留下一个带有后退按钮、内容标题和设置菜单的工具栏。我想知道只有当工具栏处于“折叠”状态时,如何更改其背景色 我所指的操作与此类似,工具栏背景颜色变为绿色: 在折叠工具栏布局下方,我有一个嵌套的滚动视图和卡片视图我想你要的是app:contentScrim <android.support.design.widget.CollapsingToolbarLayout ... app:contentScrim="?attr/col

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

我所指的操作与此类似,工具栏背景颜色变为绿色:


在折叠工具栏布局下方,我有一个嵌套的滚动视图卡片视图

我想你要的是
app:contentScrim

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

首先删除

app:contentScrim="?attr/colorPrimary"> 
从折叠工具栏布局

添加库

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));

                    }
                }
            });

只要使用
collappingtoolbarlayout
XML属性
contentScrim
设置
Toolbar
处于
collapping
模式时的背景色即可

app:contentScrim=“你的工具栏颜色”

这里有一个例子:

<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>


希望这会有所帮助~

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

    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));

            }
        }

    });
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))
            }
        }

也许你一直在寻找的是:

myCollapsingToolbar.setContentScrimColor(getResources().getColor(R.color.my_color_id));
它对我很有用,并且在折叠工具栏折叠后更改了它的颜色,以帮助我适应折叠工具栏满刻度时显示的图像的主颜色。有了这个,颜色显然可以通过编程改变


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

请给我代码好吗?你是如何制作gif的?当我旋转手机时,这个折叠工具栏布局应用程序:contentScrim=“?attr/colorPrimary”导致android.view.InflateException错误膨胀类,其他人说要替换?attr@color或@dimen等,但应用程序无法识别它们:。。。我该怎么办?“谢谢你,伊莱,斯克里姆是什么意思?”苏迪尔汉格说,我也很好奇。看起来他们指的是剧院里使用的一种设备:“一块薄纱布,在从后面发光之前是不透明的,用作屏幕或背景。”
generateAsync
已被弃用,在我这边也没有变化,尽管在删除
app:contentScrim=“?attr/colorPrimary”
后,有什么帮助吗?