虽说我们现在的标题栏是使用Toolbar来编写的, 不过它看上去和传统的ActionBar其实没什么两样, 只不过可以响应RecyclerView的滚动事件来进行隐藏和显示。 而Material Design中并没有限定标题栏必须是长这个样子的, 事实上, 我们可以根据自己的喜好随意定制标题栏的样式。
顾名思义, CollapsingToolbarLayout是一个作用于Toolbar基础之上的布局, 它也是由DesignSupport库提供的。 CollapsingToolbarLayout可以让Toolbar的效果变得更加丰富, 不仅仅是展示一个标题栏, 而是能够实现非常华丽的效果。
不过, CollapsingToolbarLayout是不能独立存在的, 它在设计的时候就被限定只能作为
AppBarLayout的直接子布局来使用。 而AppBarLayout又必须是CoordinatorLayout的子布局, 因此
本节中我们要实现的功能其实需要综合运用前面所学的各种知识。
首先我们需要一个额外的活动来作为水果的详情展示界面,右击com.example.materialtest包
→New→Activity→Empty Activity, 创建一个FruitActivity, 并将布局名指定成activity_fruit.xml,
然后我们开始编写水果详情展示界面的布局。
由于整个布局文件比较复杂, 这里我准备采用分段编写的方式。 activity_fruit.xml中的内容主要分为两部分, 一个是水果标题栏, 一个是水果内容详情, 我们来一步步实现。
接下来, 我们在CollapsingToolbarLayout中定义标题栏的具体内容, 如下所示:
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:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="250dp">
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
android:id="@+id/fruit_imag