- CompoundButton 源码分析
- LinearLayout 源码分析
- SearchView 源码解析
- LruCache 源码解析
- ViewDragHelper 源码解析
- BottomSheets 源码解析
- Media Player 源码分析
- NavigationView 源码解析
- Service 源码解析
- Binder 源码分析
- Android 应用 Preference 相关及源码浅析 SharePreferences 篇
- ScrollView 源码解析
- Handler 源码解析
- NestedScrollView 源码解析
- SQLiteOpenHelper/SQLiteDatabase/Cursor 源码解析
- Bundle 源码解析
- LocalBroadcastManager 源码解析
- Toast 源码解析
- TextInputLayout
- LayoutInflater 和 LayoutInflaterCompat 源码解析
- TextView 源码解析
- NestedScrolling 事件机制源码解析
- ViewGroup 源码解析
- StaticLayout 源码分析
- AtomicFile 源码解析
- AtomicFile 源码解析
- Spannable 源码分析
- Notification 之 Android 5.0 实现原理
- CoordinatorLayout 源码分析
- Scroller 源码解析
- SwipeRefreshLayout 源码分析
- FloatingActionButton 源码解析
- AsyncTask 源码分析
- TabLayout 源码解析
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
2.2 Layout
在 onLayout 中,我们可以看到 CoordinatorLayout 会对每一个子 View 依照以下判断顺序进行 layout:
- 如果子 View 设置了
Behavior,并且该Behavior的behavior.onLayoutChild返回true,则使用behavior.onLayoutChild对该子 View 进行 layout; - 如果 Behavior 不进行 layout,则进入自身的
onLayoutChild(),内部依次进行如下判断:
- 如果子 View 设置了
Anchor,则调用layoutChildWithAnchor(根据 anchor 进行 layout); - 如果子 View 含有
keyline,则调用layoutChildWithKeyline(根据 keyline 进行 layout); - 如果以上判断都不符合,则直接将 View 根据 padding/margin/measure 结果 按照
Gravity放置。
我们一一来看一下这些过程。
2.2.1 使用 Behavior 进行 layout
默认的 Behavior 的 onLayoutChild 都是返回 false 的,那么我们看看 FloatingActionButton 的默认 Behavior 是怎么处理的吧:
@Override
public boolean onLayoutChild(CoordinatorLayout parent, FloatingActionButton child,
int layoutDirection) {
// 检查该 FAB 是否依赖 AppBarLayout
final List<View> dependencies = parent.getDependencies(child);
for (int i = 0, count = dependencies.size(); i < count; i) {
final View dependency = dependencies.get(i);
if (dependency instanceof AppBarLayout
&& updateFabVisibility(parent, (AppBarLayout) dependency, child)) {
break;
}
}
// 调用 CoordinatorLayout 的 onLayoutChild 对 FAB 进行 layout
parent.onLayoutChild(child, layoutDirection);
// 在 API < 21 时,需要手动 offset 来让出阴影的位置
offsetIfNeeded(parent, child);
return true;
}这里主要是处理了如果 FAB 设置了 AppBarLayout 为 anchor 时(此时会对 AppBarLayout 有依赖),则当 AppBarLayout 的高度不足以显示 FAB 时将其隐藏)。
之后它会手动调用 CoordinatorLayout 自身的 onLayoutChild 方法进行 layout,即上述判断的第二步,那我们继续往下看。
2.2.2 使用 Anchor 进行 layout
如果 View 设置了 anchor,那么都会调用 layoutWithAnchor 进行 layout,代码与解释如下:
private void layoutChildWithAnchor(View child, View anchor, int layoutDirection) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final Rect anchorRect = mTempRect1;
final Rect childRect = mTempRect2;
/* 1. 找到被 anchor 的 View 的布局边界 */
getDescendantRect(anchor, anchorRect);
/* 2. 获取到被 anchor 的 View 布局边界之后,配合 layout_anchorGravity 与自身的 gravity 获取到最终要 layout 到的边界 */
getDesiredAnchoredChildRect(child, layoutDirection, anchorRect, childRect);
child.layout(childRect.left, childRect.top, childRect.right, childRect.bottom);
}这里用到的两个关键函数就是 getDescendantRect 与 getDesiredAnchoredChildRect ,它们的目的在我添加的注释中进行了解释,为保证文章的可读性就不再把代码放上来了,有兴趣的同学可以再自己去挖掘相应代码~~
2.2.3 直接 layout
如果之前的都不符合,就会走到这一步,我们看看它是怎么 layout 的:
private void layoutChild(View child, int layoutDirection) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final Rect parent = mTempRect1;
parent.set(getPaddingLeft() lp.leftMargin,
getPaddingTop() lp.topMargin,
getWidth() - getPaddingRight() - lp.rightMargin,
getHeight() - getPaddingBottom() - lp.bottomMargin);
//...(省略代码) 处理由于 fitsSystemWindows 带来的 inset
// 按照 Gravity 与 measure 尺寸在父控件里面找到自己的位置,并进行 layout。
final Rect out = mTempRect2;
GravityCompat.apply(resolveGravity(lp.gravity), child.getMeasuredWidth(),
child.getMeasuredHeight(), parent, out, layoutDirection);
child.layout(out.left, out.top, out.right, out.bottom);
}绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论