我已阅读 Snackbar文档
但不确定小吃吧parentTarget
(父视图)和anchoredView
.
如果我错了,请纠正我:
1)parent view
是小吃店从 hierchy 视图中向上走以找到suitable parent view
.
2)合适的父视图不是anchoredView。
当一个人设置 a parentView
(由ctor要求)并且还设置时会发生什么anchoredView
?
小吃店在哪里开?
我已阅读 Snackbar文档
但不确定小吃吧parentTarget
(父视图)和anchoredView
.
如果我错了,请纠正我:
1)parent view
是小吃店从 hierchy 视图中向上走以找到suitable parent view
.
2)合适的父视图不是anchoredView。
当一个人设置 a parentView
(由ctor要求)并且还设置时会发生什么anchoredView
?
小吃店在哪里开?
SnackBar
的父视图将告诉应在哪个容器或布局上绘制快餐栏。通常,snackbar 从您作为参数传递的视图中寻找协调器布局作为父级,如果找不到它,它将把活动内容布局 (android.R.id.content) Framelayout 作为父级。
代码取自SnackBar
@Nullable
private static ViewGroup findSuitableParent(View view) {
ViewGroup fallback = null;
do {
if (view instanceof CoordinatorLayout) {
// We've found a CoordinatorLayout, use it
return (ViewGroup) view;
} else if (view instanceof FrameLayout) {
if (view.getId() == android.R.id.content) {
// If we've hit the decor content view, then we didn't find a CoL in the
// hierarchy, so use it.
return (ViewGroup) view;
} else {
// It's not the content view but we'll use it as our fallback
fallback = (ViewGroup) view;
}
}
if (view != null) {
// Else, we will loop and crawl up the view hierarchy and try to find a parent
final ViewParent parent = view.getParent();
view = parent instanceof View ? (View) parent : null;
}
} while (view != null);
// If we reach here then we didn't find a CoL or a suitable content view so we'll fallback
return fallback;
}
BaseTransientBottomBar
的锚点会告诉您将在哪个视图上显示小吃吧。
代码取自BaseTransientBottomBar
private int calculateBottomMarginForAnchorView() {
if (anchorView == null) {
return 0;
}
int[] anchorViewLocation = new int[2];
anchorView.getLocationOnScreen(anchorViewLocation);
int anchorViewAbsoluteYTop = anchorViewLocation[1];
int[] targetParentLocation = new int[2];
targetParent.getLocationOnScreen(targetParentLocation);
int targetParentAbsoluteYBottom = targetParentLocation[1] + targetParent.getHeight();
return targetParentAbsoluteYBottom - anchorViewAbsoluteYTop;
}
...
/** Sets the view the {@link BaseTransientBottomBar} should be anchored above. */
@NonNull
public B setAnchorView(@Nullable View anchorView) {
this.anchorView = anchorView;
return (B) this;
}
/**
* Sets the id of the view the {@link BaseTransientBottomBar} should be anchored above.
*
* @throws IllegalArgumentException if the anchor view is not found.
*/
@NonNull
public B setAnchorView(@IdRes int anchorViewId) {
this.anchorView = targetParent.findViewById(anchorViewId);
if (this.anchorView == null) {
throw new IllegalArgumentException("Unable to find anchor view with id: " + anchorViewId);
}
return (B) this;
}
您可以在此处找到 SnackBar 的源代码Material SnackBar 和 BaseTransientBottomBar 在此处BaseTransientBottomBar