回答
要获取当前页面的布局结构并找到指定层级(如depth为25),可以通过以下步骤实现:
- 获取根布局元素:使用 auto.root获取当前窗口的根布局元素。
- 递归遍历布局树:编写递归函数遍历所有子控件,检查每个控件的 depth属性。
- 筛选目标层级:记录并返回 depth为 25 的控件。
示例代码
// 文档地址 https://docs.hamibot.com/
// 获取当前窗口的根布局元素
const root = auto.root;
// 递归遍历所有子控件并筛选指定 depth 的控件
function findControlsByDepth(node, targetDepth, result = []) {
    if (node == null) return result;
    if (node.depth === targetDepth) {
        result.push(node);
    }
    const childCount = node.childCount;
    for (let i = 0; i < childCount; i++) {
        findControlsByDepth(node.getChildAt(i), targetDepth, result);
    }
    return result;
}
// 示例:查找所有 depth 为 25 的控件
const targetDepth = 25;
const matchedControls = findControlsByDepth(root, targetDepth);
if (matchedControls.length > 0) {
    toast("找到 " + matchedControls.length + " 个 depth 为 " + targetDepth + " 的控件");
    // 输出第一个控件的详细信息(可根据需求处理)
    log(matchedControls[0].bounds);
} else {
    toast("未找到 depth 为 " + targetDepth + " 的控件");
}
注意事项
- 性能要求:该脚本需要递归遍历整个布局树,对复杂页面可能消耗较多性能,建议在必要时使用。
- Android 版本限制:auto.root和depth属性要求 Android 5.0 及以上系统(见知识库文档 5)。
- 无障碍服务:需确保无障碍服务已启用,否则 auto.root会返回null。
- 控件动态性:页面布局可能随操作动态变化,建议在目标页面稳定后再执行查找。
替代方案
如果仅需分析布局结构,可直接使用 Hamibot 的布局分析功能:
// 发送广播触发布局分析(仅调试用)
app.sendBroadcast('inspect_layout_bounds');
此操作会在屏幕上显示布局边界,辅助定位控件。
                ℹ️ 本回答由 AI 生成,内容仅供参考