MaterialDesign5.0 中 CoordinatorLayout 和 AppBarLayout
来制作悬停效果布局 。
先看下栗子
实现很简单
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.administrator.exampletest.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
>
<!--
appbarlayout 中 只要标记有
app:layout_scrollFlags="scroll"
在下部的监听滚动控件 上拉时会自动的收缩标记的内容
如果不标记的 则会悬停在顶部
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_scrollFlags="scroll"
>
<ImageView
android:src="@mipmap/ic_launcher_round"
android:layout_width="wrap_content"
android:layout_height="25dp" />
<ImageView
android:src="@mipmap/ic_launcher_round"
android:layout_width="wrap_content"
android:layout_height="25dp" />
<ImageView
android:src="@mipmap/ic_launcher_round"
android:layout_width="wrap_content"
android:layout_height="25dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#f0f"
app:layout_scrollFlags="scroll"
></View>
<TextView
android:background="#a3e2f6"
android:text="我是悬停的那个控件哦"
android:gravity="center"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="20dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<!--
需要监听滚动的控件
app:layout_behavior="@string/appbar_scrolling_view_behavior"
-->
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
布局文件使用
CoordinatorLayout
标明内部需要监听事件
AppBarLayout
标记需要折叠的布局
在需要折叠布局中 标注
app:layout_scrollFlags="scroll"
需要在顶部停留的控件 就直接写就行了。
在需要监听滑动触发折叠的控件上 标注
app:layout_behavior="@string/appbar_scrolling_view_behavior"
简单实现就酱紫
小栗子

本文介绍了如何在Android中利用MaterialDesign5.0的CoordinatorLayout和AppBarLayout创建顶部悬停折叠效果。通过设置布局和使用特定属性,可以轻松实现滑动触发的折叠行为。
1457

被折叠的 条评论
为什么被折叠?



