[英]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.