CoordinatorLayout 朋友圈 Toolbar 效果

这篇博客介绍了如何利用CoordinatorLayout、AppBarLayout和CollapsingToolbarLayout来创建类似朋友圈的Toolbar效果。详细讲解了Behavior的概念,以及如何设置layout_scrollFlags以实现不同滚动行为。还提到了CollapsingToolbarLayout的功能,如折叠Title、内容和状态栏的纱布效果,以及视差滚动和子View固定。
摘要由CSDN通过智能技术生成

Step1 简单介绍 CoordinatorLayout

  • 作用:协调子 View
  • 使用核心: Behavior
  • 布局属性类似 FrameLayout

Behavior

两个概念:

Child:CoordinatorLayout 的子 View
Dependency:Child 依赖的 View

简单使用:

device-2018-08-30-233559.gif

定义 Behavior

class SimpleBehavior(context: Context, attrs: AttributeSet) : CoordinatorLayout.Behavior<Button>(context, attrs) {
   
	private val screenWidth: Int = context.resources?.displayMetrics?.widthPixels!!
	override fun layoutDependsOn(parent: CoordinatorLayout, child: Button, dependency: View): Boolean {
   
		//判断是否依赖的View
		return dependency is WithFingerTextView
	}
	// 当依赖的 View 位置 宽高发生变化时,执行这个方法
	override fun onDependentViewChanged(parent: CoordinatorLayout, child: Button, dependency: View): Boolean {
   
		val x = screenWidth - dependency.x - child.width
		// 更新 Child
		setPosition(child, x.toInt(), dependency.y.toInt())
		return true
	}
	private fun setPosition(v: View, x: Int, y: Int) {
   
		val layoutParams: CoordinatorLayout.LayoutParams = v.layoutParams as CoordinatorLayout.LayoutParams
		layoutParams.leftMargin = x
		layoutParams.topMargin = y
		v.layoutParams = layoutParams	
	}
}
布局中使用
<android.support.design.widget.CoordinatorLayout
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<Button		
		android:layout_width="100dp"
		android:layout_height="wrap_content"
		android:text="A Button"
		app:layout_behavior="com.jy.friendcircleappbar.SimpleBehavior" /></
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值