具有默认动画的CollapsingToolbarLayout的多行标题?

问题描述

  1. activity_scrolling
<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"
    android:fitsSystemWindows="true"
    tools:context=".ScrollingActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.PopupOverlay">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:collapsedTitleGravity="center"
            app:collapsedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Collapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="0dp"
            app:expandedTitleMarginStart="0dp"
            app:expandedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Expanded"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="?actionBarSize"
                android:gravity="left|bottom"
                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="@string/app_name"
                    android:textColor="@android:color/black"
                    android:textSize="28sp" />

            </androidx.appcompat.widget.LinearLayoutCompat>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

            <androidx.appcompat.widget.LinearLayoutCompat
                android:id="@+id/llViewContainer"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                android:background="@android:color/transparent"
                android:gravity="center"
                app:layout_collapseMode="pin">

                <View
                    android:layout_width="120dp"
                    android:layout_height="5dp"
                    android:background="@android:color/holo_red_dark" />

            </androidx.appcompat.widget.LinearLayoutCompat>

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_scrolling" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
  1. ScrollingActivity.class

    findViewById<AppBarLayout>(R.id.app_bar).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 = resources.getString(R.string.app_name);
                        isShow = true
                        llViewContainer.gravity = Gravity.BottOM or Gravity.CENTER
                    } else if (isShow) {
                        //carefull there must a space between double quote otherwise it dose't work
                        collapsingToolbar?.title = " "
                        isShow = false
                        llViewContainer.gravity = Gravity.CENTER
                    }
                }
            })
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...