Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Snackbar getAnchorView()与父视图_Android_Material Design_Android Snackbar_Snackbar - Fatal编程技术网

Android Snackbar getAnchorView()与父视图

Android Snackbar getAnchorView()与父视图,android,material-design,android-snackbar,snackbar,Android,Material Design,Android Snackbar,Snackbar,我已经读过Snackbar了 但不确定snackbar的parentTarget(父视图)和anchoredView之间的区别 如果我错了,请纠正我: 1) 父视图是一个视图,snackbar从该视图向上走到Hirrchy视图,以查找合适的父视图 2) 合适的父视图不是anchoredView 当设置一个parentView(ctor需要)和anchoredView时会发生什么 小吃店在哪里开门 SnackBar的父视图将告诉您将在哪个容器或布局上运行 应绘制蛇形条。通常,snackbar会寻找

我已经读过Snackbar了

但不确定snackbar的
parentTarget
(父视图)和
anchoredView
之间的区别

如果我错了,请纠正我:

1)
父视图
是一个视图,snackbar从该视图向上走到Hirrchy视图,以查找合适的父视图

2) 合适的父视图不是anchoredView

当设置一个
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
的锚点将显示哪个视图的顶部 将显示snackbar

代码取自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;
  }

您可以在这里的SnackBar中找到源代码 这里是BaseTransientBottomBar

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
的锚点将显示哪个视图的顶部 将显示snackbar

代码取自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;
  }

您可以在这里的SnackBar中找到源代码
和这里的BaseTransientBottomBar

您自己试过吗?您自己试过吗?谢谢,您能用一个例子解释一下两者之间的区别吗
在哪个视图的顶部将显示snackbar
与在哪个容器或布局上应绘制snackbar相比。谢谢,请举例说明两者之间的区别
在哪个视图的顶部将显示snackbar
与应在哪个容器或布局上绘制snackbar相比。