简体   繁体   English

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

[英]Android Material Design - How to change background color of Toolbar after CollapsingToolbarLayout is collapsed

After the user scrolls down the screen, the image in the CollapsingToolbarLayout disappears and is left with a toolbar with the back button, content title, and settings menu. 用户向下滚动屏幕后, CollapsingToolbarLayout中的图像消失,并留下带有后退按钮,内容标题和设置菜单的工具栏。 I want to know how to change the background color of that toolbar only when its in a 'collapsed' state. 我想知道仅在工具栏处于“折叠”状态时如何更改其背景颜色。

The action I am referring to is similar to this where the toolbar background color changes to green: 我所指的操作与此类似,其中工具栏背景颜色变为绿色:

在此处输入图片说明

Below the CollapsingToolbarLayout I have a NestedScrollView with CardViews 下面CollapsingToolbarLayout我有一个CardViews NestedScrollView

I think you're after app:contentScrim . 我认为您在追求app:contentScrim

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

First remove 首先删除

app:contentScrim="?attr/colorPrimary"> 

from CollapsingToolbarLayout 来自CollapsingToolbarLayout

Add library 新增图书馆

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

And add below code in java code 并在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));

                    }
                }
            });

Just use CollapsingToolbarLayout XML attribute contentScrim to set Toolbar background color when it's in collapsed mode. 处于collapsed模式时,只需使用CollapsingToolbarLayout XML属性contentScrim来设置Toolbar背景颜色。

app:contentScrim="YOUR_TOOLBAR_COLOR"

Here is an Example: 这是一个例子:

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

Hope this will help~ 希望这会有所帮助〜

Maybe what you have been looking for is this : 也许您一直在寻找的是:

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

It worked for me and changed the color of the collapsingToolbar once it was collapsed to help me fit the main color of an image that was displayed when the collapsingToolbar was full scale. 它对我有用,并在折叠后改变了collapsingToolbar的颜色,以帮助我适应在collapsingToolbar满刻度时显示的图像的主要颜色。 With this, the color can obviously be changed programatically! 这样,显然可以通过编程更改颜色!

I know I'm late, but I hope it could help. 我知道我迟到了,但我希望能有所帮助。

You can use an AppBarLayout 's offset listener and change the CollapsingTollbar attributes according to the desired behavior. 您可以使用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))
            }
        }
    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));

            }
        }

    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将CollapsingToolbarLayout的背景保留为ToolBar的背景,即使在折叠时也是如此 - How to keep the background of CollapsingToolbarLayout as the background of ToolBar even when collapsed 如何将RecyclerView放在CollapsingToolbarLayout下面并在Android中折叠时响应工具栏? - How to put RecyclerView below CollapsingToolbarLayout and responsive to toolbar when collapsed in Android? 未折叠时Android折叠工具栏的背景颜色 - Android Collapsing toolbar background color when not collapsed 如何根据android材料设计中的图像颜色更改工具栏和状态栏颜色的颜色? - how to change the color of the toolbar and status bar colors according to image color in android material design? 我应该如何使用材料设计更改顶部工具栏中图标和标题的颜色? -NoActionBar 主题 - Android 材料设计 - How should I change the color of the icons and title in the top toolbar using material design? -NoActionBar theme - Android Material Design Android:使用ImageView工具栏背景的CollapsingToolbarLayout - Android: CollapsingToolbarLayout with ImageView Toolbar background 如何更改 Android Material ChipGroup 的背景颜色? - How to change background color of Android Material ChipGroup? Android - CollapsingToolbarLayout 折叠后用 animation 隐藏 TextView - Android - Hide TextView with animation after CollapsingToolbarLayout collapsed 如何在NavigationDrawer材质设计中更改每个菜单项的背景颜色? - How to change background color for each menu item in NavigationDrawer material design? Android - 材质设计 - 如何设置上下文菜单的背景颜色 - Android - material design - how to set the background color for a context menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM