Jetpack Compose View。已不再是替代品

问题描述 投票:0回答:1

是否有任何 compose View.GONE 替代方案,这不是条件? 我需要一个按钮仍然可以通过自动化测试找到和点击,但不能被用户找到和点击。

所以使用条件对我来说不是解决方案。

if (false) {
    Button(
        onClick = { onDebugClicked() },
        modifier = Modifier.addResourceIdWithPackageNameAndContentDesc("buttonDebug")
    ) {
        // this does not create the button at all so it cannot be clicked by a script
    }
}

我也不希望它通过使用 alpha 来隐形,我希望它消失

我也尝试缩小它,使其没有尺寸,但这也不起作用

Box(
    modifier = Modifier
        .size(0dp)
        .wrapContentSize()
        .addResourceIdWithPackageNameAndContentDesc("buttonDebug")
        .clickable { onDebugClicked() },
) {
    // nothing, empty invisible clickable view for automated testing
}
android kotlin automated-tests android-jetpack-compose ui-automation
1个回答
0
投票

没有正式的方法可以做到这一点,因为您最好使用 if 条件。创建用户不可见的修饰符和可组合项只会给编译器带来不必要的工作,并且会不必要地减慢 UI 速度。

但是,如果您真的想这样做,您可以创建一个布局修改器,仅将大小返回为 0,0。

fun Modifier.gone(gone: Boolean) = if (!gone) this else this.then(Gone)

private object Gone : LayoutModifier {
    override fun MeasureScope.measure(
        measurable: Measurable,
        constraints: Constraints
    ): MeasureResult {
        return layout(0, 0) {}
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.