【问题标题】:How can I update the toolbar when scrolling with CollapsingToolbar使用 CollapsingToolbar 滚动时如何更新工具栏
【发布时间】:2022-01-08 05:17:21
【问题描述】:

我正在尝试从一个TextView 移动到Toolbar,当它在滚动时崩溃。

假设我有一个工具栏,就在它的下方我有一个视图,当我向上滚动并且它开始根本不可见时,我想用此文本更新工具栏,这是一个示例。

第一个场景

第二种情况

我一直在阅读CollapsingToolbar,但我可以弄清楚当视图不那么可见时,如果我必须创建一个自定义工具栏,在我可以设置的右侧有一个文本,因为它不是标题,它是标题的额外值。

【问题讨论】:

    标签: android kotlin android-layout android-toolbar android-collapsingtoolbarlayout


    【解决方案1】:

    哪里可以设置,因为不是标题,是标题的额外值。

    您可以将自定义Toolbar 的标题用作supportActionBar;但是将滚动标志适当地调整到工具栏和布局行为到主布局。

    如果我必须像自定义工具栏一样创建右侧的文本

    将标题设置在右侧;有 2 个属性,一个用于折叠文本,另一个用于展开文本,您需要将它们对齐到末尾/右侧:

    app:collapsedTitleGravity="end"
    app:expandedTitleGravity="end"
    

    这是一个演示:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbarlayout"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:paddingEnd="8dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:collapsedTitleGravity="end"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleGravity="end"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:title="Hi">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    app:layout_collapseMode="parallax" />
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|enterAlways|snap"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:title="Hi"
                    app:titleTextColor="#fff" />
    
            </com.google.android.material.appbar.CollapsingToolbarLayout>
    
        </com.google.android.material.appbar.AppBarLayout>
    
    
        <!--    Main Layout -->
    
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/appbarlayout">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/long_text" />
    
        </androidx.core.widget.NestedScrollView>
    
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    确保使用 NoActionBar 主题,并将自定义工具栏设置为supportActionBar

    val toolbar = findViewById<Toolbar>(R.id.toolbar)
    setSupportActionBar(toolbar)
    

    更新:

    也许我没有解释清楚这个问题,问题是,我有一个工具栏和 就在下面,我有一个带有文本的 LinearLayout,当我滚动时 这个 LinearLayout 会隐藏我想显示它的文本 在线性布局中。所以这个想法是,像你一样,但是 而不是在工具栏中有这个“嗨”,只是在一个视图中 在工具栏下方,当我滚动时它开始变得不可见/ 不可读的只是把这个“嗨”文本放在工具栏中。

    您可以使用自定义的 TextViewCollapsingToolBar.. 的标题来执行此操作,并使用 OnOffsetChangedListener 以编程方式切换两者的可见性

    首先,在折叠和展开状态下创建以下样式:

        <style name="Title.Collapsed" parent="android:TextAppearance">
            <item name="android:textColor">@android:color/white</item>
            <item name="android:textSize">18sp</item>
        </style>
    
        <style name="Title.Expanded" parent="android:TextAppearance">
            <item name="android:textColor">@android:color/white</item>
            <item name="android:textSize">28sp</item>
        </style>
    

    这些样式将分别应用于CollapsingToolbarLayout 上的app:collapsedTitleTextAppearanceapp:expandedTitleTextAppearance

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:fitsSystemWindows="true"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:collapsedTitleGravity="end"
                app:collapsedTitleTextAppearance="@style/Title.Collapsed"
                app:expandedTitleGravity="end"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleTextAppearance="@style/Title.Expanded"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="40dp"
                    android:background="@android:color/transparent"
                    android:gravity="end"
                    android:orientation="vertical"
                    android:padding="10dp"
                    app:layout_collapseMode="parallax">
    
                    <TextView
                        android:id="@+id/tv_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Hi"
                        android:textSize="28sp" />
                </LinearLayout>
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/design_default_color_primary"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/appBarlayout">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/long_text" />
    
        </androidx.core.widget.NestedScrollView>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    检测addOnOffsetChangedListener中的appBarLayout滚动变化:

    val collapsingToolbar = findViewById<CollapsingToolbarLayout>(R.id.collapsingToolbar)
    collapsingToolbar.title = ""
    title = ""
    
    val appBarLayout = findViewById<AppBarLayout>(R.id.appBarLayout)
    appBarLayout.addOnOffsetChangedListener(object : OnOffsetChangedListener {
        var isShow = false
        var scrollRange = -1
        override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
            if (scrollRange == -1) {
                scrollRange = appBarLayout.totalScrollRange
            }
            if (scrollRange + verticalOffset == 0) {
                //when collapsingToolbar at that time display actionbar title
                collapsingToolbar.title = "Hi"
                isShow = true
            } else if (isShow) {
                collapsingToolbar.title = ""
                isShow = false
            }
        }
    })
    

    【讨论】:

    • 感谢您的回答@Zain,但也许我误解了这个问题,问题是,我有一个工具栏,就在下面,我有一个带有文本的 LinearLayout,当我滚动时,这个 LinearLayout 是要隐藏我想显示它在 LinearLayout 中的文本。
    • 所以想法是,就像你做的那样,但不是在工具栏中有这个“嗨”,而是在工具栏下方的一个视图中,当我滚动时它开始变得不可见 /不可读只需将这个“嗨”文本放在工具栏中。
    • 我已经更新了问题...如果现在更清楚了,请告诉我。
    • 让我考虑一下
    • 但这并不意味着必须从左到右,文本可以在右边然后出现在工具栏的右边。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    相关资源
    最近更新 更多