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

标签 android android-toolbar android-appbarlayout android-collapsingtoolbarlayout

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

我已经能够更改滚动条上的颜色,但只能与高度更改结合使用。我试过在工具栏、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/

相关文章:

android - 完成 Firebase 函数(Uri 和 String)Android Java 时出现 ClassCastException

android - 作为 self 管理的 ActionBar 的工具栏与作为框架管理的 ActionBar 的工具栏

java - 安卓工作室 : How to prevent up button in toolbar from pushing child view to the right?

android - 在三星 Galaxy Note Edge 上扩展类 android.support.design.widget.AppBarLayout 时出错

android - CoordinatorLayout + CollapsingToolbarLayout 在状态栏下方滚动

php - 我必须点击登录按钮两次才能登录我的 Android 应用程序

android - 在指定位置找不到 NDK

android - 如何防止创建多个 Dialog 实例,以便我可以干净地解雇()?

android - 如何根据文本长度以编程方式更改工具栏高度和字体大小

android - 迁移到 AndroidX 后 AppBarLayout 膨胀错误