android 滑动顶部变化,Android实现滑动到顶部悬停的效果

先来看下要实现效果图:

ea4d2e7c87da7255ce2e2112b7175b4d.gif

查阅资料后,发现网上大部分都是用这种方法实现的:

多写一个和需要悬浮的部分一模一样的layout,先把浮动区域的可见性设置为gone。当浮动区域滑动到顶部的时候,就把浮动区域B的可见性设置为VISIBLE。这样看起来就像悬浮在顶部不动了。

这里介绍的是另外一种方式:

使用design包中的控件

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"

tools:context="com.peipei.app.MainActivity">

android:layout_width="match_parent"

android:layout_height="250dp">

app:layout_scrollFlags="scroll"

app:contentScrim="#000000"

android:layout_width="match_parent"

android:layout_height="220dp">

android:text="banner区域"

android:gravity="center"

android:textColor="#ffffff"

android:background="#987545"

android:layout_width="match_parent"

android:layout_height="220dp"/>

android:gravity="center"

android:layout_width="match_parent"

android:layout_height="30dp"

android:text="悬浮的部分"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="@dimen/text_margin"

android:text="@string/large_text"/>

实现步骤:

1、将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLayout之内

2、将CollapsingToolbarLayout的app:layout_scrollFlags设置为scroll

3、给滚动的NestedScroolView设置

app:layout_behavior="@String/appbar_scrolling_view_behavior"

就大功告成了(记得根布局要是CoordinatorLayout)

最终效果:

25fcbeeb62cf3866b78c3677f5f06266.gif

总结

以上就是关于Android滑动到顶部悬停效果的全部内容,大家都学会了吗?希望这篇文章对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值