问题描述
我将 constrainAs 与 Jetpack Compose 结合使用,以将 wifi 选项列表限制到父视图的顶部,然后限制到文本视图的底部.从照片中可以看出,我的列表没有被限制在父级的顶部或它下面的 textview,它甚至被向上推离屏幕?
I'm using constrainAs with Jetpack Compose to constrain a list of wifi options to the top of the parent and then to the bottom of a text view. As seen from the photo my list isn't being constrained to the top of the parent or to the textview below it, and it is even being pushed off the screen upwards?
参考'list'是wifi选项列表,'text1'是以Select your wifi"开头的文本视图
For reference 'list' is the list of wifi options, and 'text1' is the textview that starts with "Select your wifi"
@Composable
fun ScanWifiScreen(wifiList: List<WifiOption>, onClick: (WifiOption) -> Unit) {
ConstraintLayout(
modifier = Modifier
.fillMaxSize()
.background(colorResource(id = R.color.background))
) {
val (list, text1, text2, progressIndicator) = createRefs()
WifiList(
wifiOptions = wifiList,
onClick = onClick,
modifier = Modifier
.constrainAs(list) {
top.linkTo(parent.top, margin = 8.dp)
bottom.linkTo(text1.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
}
.background(colorResource(id = R.color.background))
.fillMaxHeight())
Text(
text = stringResource(id = R.string.select_wifi),
modifier = Modifier
.wrapContentSize()
.padding(bottom = 16.dp)
.constrainAs(text1) {
bottom.linkTo(text2.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
},
style = TextStyle(
fontFamily = FontFamily(Font(R.font.quicksand_regular)),
fontSize = 20.sp,
color = colorResource(id = R.color.main_text),
letterSpacing = 0.22.sp,
textAlign = TextAlign.Center,
lineHeight = 32.sp
)
)
Text(
text = stringResource(id = R.string.cant_see_network),
modifier = Modifier
.wrapContentSize()
.padding(bottom = 32.dp)
.constrainAs(text2) {
bottom.linkTo(progressIndicator.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
},
style = TextStyle(
fontFamily = FontFamily(Font(R.font.quicksand_regular)),
fontSize = 16.sp,
color = colorResource(id = R.color.sub_text),
letterSpacing = 0.18.sp,
textAlign = TextAlign.Center,
lineHeight = 24.sp
)
)
ProgressIndicator2(
progression = 3,
modifier = Modifier.constrainAs(progressIndicator) {
bottom.linkTo(parent.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
})
}
}
推荐答案
在你的 List 中移除 .fillMaxHeight()
修饰符并添加约束 height = Dimension.fillToConstraints
In your List remove the .fillMaxHeight()
modifier and add the constraint height = Dimension.fillToConstraints
WifiList(
//....
modifier = Modifier
.constrainAs(list) {
top.linkTo(parent.top, margin = 8.dp)
bottom.linkTo(text1.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
height = Dimension.fillToConstraints
}
)
这篇关于Jetpack Compose 约束布局约束不链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!