gpt4 book ai didi

android - 在不折叠的情况下更改滚动时的工具栏颜色

转载 作者:行者123 更新时间:2023-11-29 02:41:43 25 4
gpt4 key购买 nike

我试图让我的工具栏在滚动时改变颜色,而不是真正折叠它(所以我不希望它的高度改变)。我希望它最初是一种颜色,然后当你滚动时它会变成另一种颜色。当我滚动回到顶部时,它应该会变回原来的颜色。

我已经能够更改滚动条上的颜色,但只能与高度更改结合使用。我试过在工具栏、CollapsingToolbarLayout 和 AppBarLayout 上设置 minHeight,但不幸的是没有成功。

我知道我可能可以手动执行此操作(在滚动更改时为颜色设置动画)但我想排除先使用设计库执行此操作的可能性。

这是我目前所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/app_secondary_darker">

<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">

<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:contentScrim="@color/white"
app:layout_scrollFlags="scroll">

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

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

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

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="Abc"
android:background="@color/grey_light"/>

<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="Def"
android:background="@color/red_dark"/>

</LinearLayout>

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

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

提前致谢!

最佳答案

希望对您有所帮助。

app_bar.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
val collapsedPercent = -verticalOffset / appBarLayout.totalScrollRange.toFloat()

//仅在这 0.3的区间里交互 //这个值 在 (0,1]之间,可以自定义
//这里表示仅在收缩到仅剩百分比hotPercent的时候,才进行收缩
val hotPercent = 0.3F

val collapsedHotPercent = MathUtils.clamp(collapsedPercent / hotPercent + 1 - 1 / hotPercent, 0F, 1F)

Log.d(TAG, "onCreate collapsedPercent:" + collapsedPercent)

tabs.apply {
val normalColorStart = ContextCompat.getColor(context, R.color.k6_main_tabs_normal_start)
val normalColorEnd = ContextCompat.getColor(context, R.color.k6_main_tabs_normal_end)
val selectedColorStart = ContextCompat.getColor(context, R.color.k6_main_tabs_selected_start)
val selectedColorEnd = ContextCompat.getColor(context, R.color.k6_main_tabs_selected_end)

val normalColor = ColorUtil.getColorOfDegradate(normalColorStart, normalColorEnd, collapsedHotPercent)
val selectedColor = ColorUtil.getColorOfDegradate(selectedColorStart, selectedColorEnd, collapsedHotPercent)

setTabTextColors(normalColor, selectedColor)
setSelectedTabIndicatorColor(selectedColor)
}
}

关于android - 在不折叠的情况下更改滚动时的工具栏颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43605698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com