jetpack compose 将可组合性切成两半

Moh*_*qer 5 android android-jetpack-compose

如何在 jetpack compose 中剪切如下图所示的可组合TextButton

在此输入图像描述

ngl*_*ber 11

您可以使用drawWithContent修饰符。

Button(
    onClick = {},
    modifier = Modifier.drawWithContent {
        if (layoutDirection == LayoutDirection.Rtl) {
            clipRect(left = size.width / 2f) {
                this@drawWithContent.drawContent()
            }
        } else {
            clipRect(right = size.width / 2f) {
                this@drawWithContent.drawContent()
            }
        }
    }
) {
    Text("some text")
}
Run Code Online (Sandbox Code Playgroud)

注意到我添加了对 RTL 语言的支持,从而切断了组件的左侧。