美文网首页
获取Activity视图层级的最大深度

获取Activity视图层级的最大深度

作者: Solang | 来源:发表于2020-01-08 11:32 被阅读0次
public static int getViewHierarchyMaxDeep(Activity activity) {
    View decorView = activity.getWindow().getDecorView();
    int height = getDeep(decorView, 1);
    Log.e("TAG", activity.getClass().getSimpleName() + " height is:" + height);
    return height;
}

// curr为当前View的层级
private static int getDeep(View view, int curr) {
    if (view instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) view;
        int childCount = parent.getChildCount();
        if (childCount > 0) {
            // 层级+1
            int max = curr + 1;
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                int height = getDeep(child, curr + 1);
                if (max < height) {
                    max = height;
                }
            }
            return max;
        } else {
            return curr;
        }
    } else {
        return curr;
    }
}

相关文章

网友评论

      本文标题:获取Activity视图层级的最大深度

      本文链接:https://www.haomeiwen.com/subject/siojactx.html