Android无障碍遍历view的层级


    /**
     * 打印Tree的层次结构
     *
     * @param root view
     * @param deep 层级  用于缩进的
     */
    private String getViewTreeStr(AccessibilityNodeInfo root, int deep) {
        StringBuilder builder = new StringBuilder();
        if (root != null) {
            builder.append(getSpace(deep)).append("<").append(root.getClassName());
            builder.append(">\n");
            int childCount = ((AccessibilityNodeInfo) root).getChildCount();
            for (int i = 0; i < childCount; i++) {
                builder.append(getViewTreeStr(((AccessibilityNodeInfo) root).getChild(i), deep + 1));
            }
            builder.append(getSpace(deep)).append("\n");
        }
        return builder.toString();
    }

    private String getSpace(int deep) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < deep; i++) {
            s.append("\t");
        }
        return s.toString();
    }

使用: String tree = getViewTreeStr(rootNode,2);

你可能感兴趣的:(android,c++,java,数据结构)