【问题标题】:CollapsingToolbar - How do I disable the title animation?CollapsingToolbar - 如何禁用标题动画?
【发布时间】:2020-01-04 21:25:44
【问题描述】:

我正在尝试在折叠工具栏展开或折叠时显示标题。然而,在这些状态中,标题总是有一个移动的动画:

如何彻底摆脱动画,让标题保持在一个位置?

我尝试将 titleEnabled 设置为 false,但这只会禁用 title 而不再显示

https://stackoverflow.com/a/35975029/11110509

<android.support.design.widget.CollapsingToolbarLayout
        app:titleEnabled="false"
        ...
>

Edit1:我的完整布局: _______________________________________________________________________________

    <?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:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <com.google.android.material.appbar.AppBarLayout
       android:id="@+id/appbar_layout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@color/colorWhite">

      <com.google.android.material.appbar.CollapsingToolbarLayout
          android:id="@+id/collapsing_toolbar"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:contentScrim="@color/standardBlue"
          app:title="Title"
          app:layout_scrollFlags="scroll|exitUntilCollapsed">

         <RelativeLayout
             android:id="@+id/activityprofile_topsection"
             android:layout_width="match_parent"
             android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/activityprofile_coverpicture"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:background="@color/standardBlue" />


            <ImageView
                android:id="@+id/activityprofile_profilepicture"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="70dp"
                android:background="@drawable/profile_picture_white_border" />

            <TextView
                android:id="@+id/activityprofile_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/activityprofile_profilepicture"
                android:layout_marginStart="15dp"
                android:layout_marginTop="5dp"
                android:text="Username"
                android:textColor="@color/colorBlackFont"
                android:textSize="16sp" />

         </RelativeLayout>

         <androidx.appcompat.widget.Toolbar
             android:layout_width="match_parent"
             android:layout_height="?attr/actionBarSize"
             app:contentInsetStart="0dp"
             app:layout_collapseMode="pin">

            <ImageView
                android:id="@+id/activityprofile_backbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?attr/actionBarItemBackground"
                android:padding="15dp"
                app:srcCompat="@drawable/icon_back_white_arrow" />
         </androidx.appcompat.widget.Toolbar>

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

      <com.google.android.material.tabs.TabLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

         <com.google.android.material.tabs.TabItem
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:icon="@drawable/icon_back_black_arrow" />

         <com.google.android.material.tabs.TabItem
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:icon="@drawable/ic_heart" />
      </com.google.android.material.tabs.TabLayout>

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


   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我一添加app:titleEnabled="false",标题就消失了

仍然无法禁用 CollapsingToolbar 的标题动画。

【问题讨论】:

  • 您是否尝试过使用 API 并查看它是否可以帮助您实现您想要的? developer.android.com/reference/android/support/design/widget/… 否则我会说你需要使用另一个 API 或不同 android 框架部分的组合来重新实现。
  • 您是在Toolbar 还是CollapsingToolbarLayout 上设置标题?
  • 折叠工具栏

标签: android android-collapsingtoolbarlayout


【解决方案1】:

我相信您的布局可能会出现错误。我检查了这个行为,当你添加app:titleEnabled="false" 时,标题应该保持在同一个地方,没有任何动画。

app:titleEnabled="true"(或不带此属性)

app:titleEnabled="false"

如您所见,这是您想要实现的行为。下面我添加了一个布局的骨架,它应该可以正常工作。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    >

  <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@android:color/transparent"
      android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
      >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="@dimen/toolbar_elevation"
        app:contentScrim="@color/colorWhite"
        app:titleEnabled="false"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >

      <RelativeLayout
          android:id="@+id/it_could_be_any_layout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/profile_bg"
          android:scaleType="centerCrop"
          app:layout_collapseMode="parallax"
          >

       <!-- Your content here -->
       
      </RelativeLayout>

      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbarProfile"
          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>

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <!-- Your RecyclerView here or even replace this LinearLayout -->
    </LinearLayout>

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

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

我认为你应该再次检查你的实现并使用layout_behaviorapp:layout_scrollFlagscollapseMode 属性等等。我希望你能解决这个问题。

【讨论】:

  • 我在上面发布了我的布局。每次我添加 app:titleEnabled="false" 时,标题都会完全消失。你在哪里设置标题"Profile Fragment"
  • 以编程方式在片段中,使用toolbar.setTitle("")
  • 你的折叠工具栏没有渐变效果
  • 请再看一遍我发布的 gif。褪色效果就在那里。这是CollapsingToolbarLayout的默认效果。
  • 放弃后,又试了一次,2个月后终于搞定了。这需要设置app:titleEnabled="false" 并且toolbar.setTitle(" ") 需要是常规工具栏而不是折叠工具栏标题的工具栏。这就是动画没有被禁用的原因......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 2011-06-16
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多