【发布时间】:2015-09-17 08:41:59
【问题描述】:
如何设计 Appbar 从屏幕中间滚动并固定在顶部的布局?
我的问题与 Google I/O 2015 应用程序中的设计完全相同(活动详情屏幕)。我使工具栏出现在与顶部的偏移处,并在顶部有一个 ImageView。还有一个包含所需内容的 ScrollView。现在,只有 ScrollView 中的内容被滚动,而不是 ImageView 或 Toolbar。我还需要在 Parallax 的顶部滚动 ImageView。
有人可以帮我设计布局吗?
activity_detail.xml
<RelativeLayout
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">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="196dp"
android:layout_alignParentTop="true"
android:src="@mipmap/ic_launcher" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@id/imageView"
android:background="@color/blue"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetLeft="72dp"
app:contentInsetStart="72dp"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:text="Project Tango = Mobile 3D tracking and perception"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:layout_marginRight="16dp"
android:text="29 May 10:00-11:00 am in Room 2 (L2)"
android:textColor="#FFFFFF"
android:textSize="14sp" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_below="@id/appbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/longText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</RelativeLayout>
【问题讨论】:
-
将所有内容放在一个 ScrollView 中并覆盖
ScrollView的 onScrollChanged。在那里,您应该将ImageView向下滚动一半。这种效果称为视差。 -
@SemyonDanilov 我将整个 RelativeLayout 放在 ScrollView 中并删除了现有的。但是,一旦 Appbar 到达顶部,我该如何固定它?
标签: android android-toolbar android-coordinatorlayout android-appbarlayout