跳到主要内容

控件深度 (Selector.depth)

Selector.depth

声明

node.selector():depth(val)

参数

参数类型是否必填说明
valnumber控件的层级深度

返回值

类型说明
Selector返回控件查找器,用于链式编程

说明

通过控件在控件树中的层级深度约束控件。

属性含义:

  • depth 表示控件在控件树中的嵌套层级
  • 根控件(通常是 DecorView)的 depth 为 0 或 1
  • 每向下一层子控件,depth 增加 1

适用场景:

  • 配合其他约束精确定位特定层级的控件
  • 过滤掉嵌套过深或过浅的控件
  • 在复杂布局中区分同类型但不同层级的控件

典型应用:

  • 只查找顶层容器:depth(1)depth(2)
  • 查找特定嵌套深度的控件
  • 优化查找性能,限定搜索深度

注意事项:

  • depth 值因应用布局结构而异,需先用 dump() 分析
  • 不同设备或不同版本的应用,depth 可能不同
  • 通常与 type、id 等约束组合使用更有效
  • 单独使用 depth 约束可能匹配到大量控件
待实现

该接口正在开发中。

示例

local node = require("node")

-- 查找深度为 4 的控件
local n = node.selector():depth(4):find()

if n then
print(node)
else
print("没有找到任何控件")
end

AScript 对应

# AScript (Python)
from ascript.android.node import Selector

node = Selector().depth(4).find()