我想在Jetpack中行的末尾设置RadioButton组件。尝试使用约束布局并将RadioButton移出行之外,但是RadioButton没有与行中的其他组件集中。我该怎么办?

这是我的代码:
ConstraintLayout {
val (row, button) = createRefs()
Row(
modifier = Modifier
.height(56.dp)
.fillMaxWidth()
.constrainAs(row){
start.linkTo(parent.start)
end.linkTo(parent.end)
},
verticalAlignment = Alignment.CenterVertically
) {
Icon(
/* *** */
)
Text(
text = "mail@gmail.com",
modifier = Modifier.padding(start = 16.dp, end = 16.dp),
)
RadioButton(
/* *** */
)
}
}更重要的是,如果文本太长,我希望剪切文本组件(而不是覆盖或放置Radio )。
发布于 2022-03-23 23:16:34
最简单的解决方案是在文本和单选按钮之间添加一个带有Spacer的Modifier.weight(1f)。Row和Column根据组件的权重在具有weight修饰符的组件之间分配剩余可用空间。由于只有一个,它将接收所有剩余的空间,按单选按钮到极右。
例如,下面的代码将产生您想要的行为:
Row(modifier = Modifier.height(56.dp).fillMaxWidth(), verticalAlignment = Alignment.CenterVertically){
Icon(Icons.Default.Add,null)
Text("Some text here")
Spacer(Modifier.weight(1f).fillMaxHeight().background(Color.Green)) // height and background only for demonstration
RadioButton(selected = false, onClick = { /*TODO*/ })
}

正如我所说的,剩余的空间是根据每个元素的权重分布的,所以尽管这不是您想要实现的,但是这个例子可能是这样的。
Row(modifier = Modifier.height(56.dp).fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
Icon(Icons.Default.Add, null)
Spacer(Modifier.weight(1f).fillMaxHeight().background(Color.Red)) // height and background only for demonstration
Text("Some text here")
Spacer(Modifier.weight(4f).fillMaxHeight().background(Color.Green)) // height and background only for demonstration
RadioButton(selected = false, onClick = { /*TODO*/ })
}会让你

测量图标、文本和单选按钮后的剩余空间分配为红色空格20%,绿色空间80%,因为这是它们在总重量中所占的份额(1/5和4/5)。
发布于 2022-03-23 21:54:51
您可以创建一个Box来填充行的其余部分,并将Button放入其中。然后,可以将Button对齐到右边。
Box(modifier = Modifier.fillMaxWidth()) {
RadioButton(modifier = Modifier.align(Alignment.End)){}
}对于列(而不是Box ),也可以实现同样的目的,但是行中的每个单独的元素都应该有一个列来包装它。
https://stackoverflow.com/questions/71594277
复制相似问题