我想在樂趣中創建一個自定義可組合項,然后將該可組合項添加到另一個樂趣中,其中包含我的主要 ConstraintLayout 以及布局的所有可組合項。當我嘗試執行此操作時遇到約束問題,我將可組合項放入 ConstraintLayout,但它卡在左上角/開始角。
如何正確地將約束添加到這個構建并來自另一個樂趣的可組合項到我的主要 ConstraintLayout 樂趣中?
我的代碼:
@Composable
fun MainConstraintLayout() {
ConstraintLayout {
val (view1, view2) = createRefs()
Button(onClick = { /*....*/ },
modifier = Modifier.constrainAs(view1) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
}
) { Text("view1, correct constraints") }
// How can I constraint this view (below) to the bottom of view1?
MyOneComposable()
}
}
@Composable
fun MyOneComposable() {
Button(onClick = { /*....*/ }) {
Text(text = "My one composable button")
}
}
uj5u.com熱心網友回復:
您需要將Modifier引數傳遞到您的MyOneComposable:
@Composable
fun MyOneComposable(
modifier: Modifier,
) {
Button(onClick = { /*....*/ }, modifier = modifier) {
Text(text = "My one composable button")
}
}
所以你可以應用 ref 修飾符ConstraintLayout:
MyOneComposable(Modifier.constraintAs(view2) { ... })
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/419785.html
標籤:
上一篇:顫動底部導航欄專案未對齊
