java - 工具栏图像动画折叠样式

标签 java android xml

我有一个 Toolbar,当我进入 Activity 时,图像会展开,Toolbar 标题位于图像底部,当我滚动 Toolbar 下方的内容时,图像消失并且工具栏标题移动到顶部位置。

我能解释的最简单的方法是在附图中: Scroll up example

我目前的代码如下:

<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:id="@+id/coordinatorLayout_list_item"
android:fitsSystemWindows="true"
tools:context=".ui.activities.ItemBuyNowActivity">


  <android.support.design.widget.AppBarLayout
  android:id="@+id/appbar"
  android:layout_width="match_parent"
  android:background="@color/orange_6"
  android:layout_height="@dimen/detail_backdrop_height"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar_buy_now_list_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginStart="48dp"
    app:expandedTitleMarginEnd="64dp">

      <android.support.v7.widget.Toolbar
      android:id="@+id/buy_now_tool_bar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
      app:layout_collapseMode="parallax" />

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

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

  <android.support.design.widget.FloatingActionButton
  android:id="@+id/list_item_cart_fab"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom|end"
  android:layout_margin="@dimen/fab_margin"
  android:src="@drawable/cart_empty_dark"/>

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

编辑:遗失已久的问题是,我希望我的工具栏显示在图像下方,当向上滚动时它会停在顶部并且图像会消失。

目前,标题位于图像的底部但覆盖了图像,当滚动到顶部时,标题位于顶部并且图像已经按照预期消失了。

问题是当工具栏完全展开时,我需要将工具栏放在图像下方!

谢谢

最佳答案

好的,这已经很简单了。

将您当前的代码替换为:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="16dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

           <!-- This is the image you want to add -->

        <ImageView
            android:id="@+id/iv"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

           <!-- Toolbar to just hold the action icons -->

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

           <!-- Nested scroll to make the content under the bar scrolls and move up and down the collapsing toolbar -->

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

           <!-- YOUR CONTENT -->

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

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

关于java - 工具栏图像动画折叠样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35173017/

相关文章:

java - 在哪里可以找到特定于应用程序的 context.xml 文件?

java - 按键释放的 Android 键盘问题

android - 中文 android webView 中字符膨胀的问题

java - 如何使用此路径读取 xml 字符串值,如代码所示(getFilesDir().getAbsolutePath()+ File.separator + "test.xml")

java - 作为 IN 运算符参数的对象列表

android - 关于android drawable的奇怪问题

java - Android Espresso 无法输入 TYPE_TEXT_VARIATION_NORMAL?

c# - 我们如何使用 C# 读取 .docx 文件?

xml - R : "subscript out of bounds" 中的数据混搭

java - 如何在 hibernate 中更新分离的对象