【问题标题】:Collapsing Toolbar image disappearing when scolling up向上滚动时折叠工具栏图像消失
【发布时间】:2017-10-05 01:19:34
【问题描述】:

我正在尝试实现一个带有大标题图像的折叠工具栏。我希望图像开始时非常大(并且有效)而不是完全崩溃(这也有效)。问题是,当工具栏达到最小可折叠高度时,图像会消失,褪色为应用程序的原色。 我希望图像即使在折叠时也能保持可见。

另外,后退按钮与图片一起向上滚动,我希望它保持固定。

活动的 xml:

    <android.support.design.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"
                                            android:fitsSystemWindows="true">         <android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:id="@+id/imageViewToolbar"
        android:minHeight="500dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
        app:layout_collapseMode="parallax" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:title=""
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:layout_scrollFlags="scroll|enterAlways"
         />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/detail_content"/>

谢谢。

【问题讨论】:

    标签: android android-layout material-design android-toolbar


    【解决方案1】:

    我修好了:

    我通过将 app:statusBarScrim="@android:color/transparent" 添加到 CollapsingToolbarLayout 使图像不会褪色为纯色,并通过将 app:layout_collapseMode="pin" 添加到工具栏的布局来修复后退按钮向上滑动。

    【讨论】:

      【解决方案2】:

      使用此代码:

      <android.support.design.widget.CollapsingToolbarLayout
              android:id="@+id/collapse_toolbar"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_scrollFlags="scroll|exitUntilCollapsed"
              android:fitsSystemWindows="true">
      
              <ImageView
                  android:id="@+id/bgheader"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:scaleType="centerCrop"
                  android:fitsSystemWindows="true"
                  android:background="@drawable/sunflowerpic"
                  app:layout_collapseMode="pin" />
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/MyToolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  app:layout_collapseMode="parallax" />
      
          </android.support.design.widget.CollapsingToolbarLayout>
      

      向上滚动后,这里是输出的样子。图像被固定在顶部作为应用栏的背景。

      这是一个很好的Example

      【讨论】:

        【解决方案3】:

        您可以添加偏移量更改侦听器,以在达到大于百分比时显式设置您的操作栏。 (有时我不知道为什么,滚动行为的偏移量会发生变化,即使它具有固定的高度)

        在 Kotlin 中:

        app_bar_layout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
                val percentage: Float = Math.abs(verticalOffset) / appBarLayout.totalScrollRange.toFloat()
                Log.d("%", "$percentage")
                when {
                    percentage < 0.1 ->  (activity as MainActivity).bottom_navigation.visibility = View.GONE
                    percentage >= 1.0.toFloat() -> (activity as MainActivity).setSupportActionBar(where_toolbar) // This is hack to avoid toolbar disappearing
                    else ->  (activity as MainActivity).bottom_navigation.visibility = View.VISIBLE
        
                }
            })
        

        这里我用它来将它膨胀成一个片段。

        【讨论】:

          猜你喜欢
          • 2016-01-26
          • 1970-01-01
          • 1970-01-01
          • 2015-10-28
          • 2017-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多