[英]Android - Adding custom Modifiers in jetpack
我想添加一些語義
contentDescription = "SomeID"
testTag = "SomeID"
一些 UI 元素
目前的做法是這樣的
modifier = Modifier.padding(top = 10).semantics {
testTag = "SomeID"
contentDescription = "SomeID"
},
如何編寫接收輸入數據並將其分配給語義的自定義擴展
modifier = Modifier.padding(top = 10).addSemantics(id = "SomeID"),
你可以這樣做
fun Modifier.customModifier(paddingValues: PaddingValues, description: String) = this.then(
padding(paddingValues).semantics {
testTag = description
contentDescription = description
}
)
當您希望您的修改器具有 memory 或訪問 Comopsable scope 以使用 LaunchedEffect 等時,您可以使用composed
as
fun Modifier.customModifierWithMemory(paddingValues: PaddingValues, description: String) =
composed {
LaunchedEffect(key1 = Unit){
// Do something here
}
var memory by remember {
mutableStateOf(0)
}
Modifier
.padding(paddingValues)
.semantics {
testTag = description
contentDescription = description
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.