Android Compose中的列权重分布
我有一个Android Compose中的列权重分布,android,android-jetpack-compose,Android,Android Jetpack Compose,我有一个列组件(setingsgraphicselectcomposeview.kt),它的重量需要为1f(modifier.weight(weight=1f)),这样容器中的多个组件才能均匀分布。问题是,当将Compose从alpha02升级到alpha06时,将上述修饰符指定给列已不再可能 以下是组件代码: @Composable fun SettingsGraphicSelectComposeView( modifier: Modifier = Modifier, text
列
组件(setingsgraphicselectcomposeview.kt),它的重量需要为1f(modifier.weight(weight=1f)
),这样容器中的多个组件才能均匀分布。问题是,当将Compose从alpha02升级到alpha06时,将上述修饰符指定给列已不再可能
以下是组件代码:
@Composable
fun SettingsGraphicSelectComposeView(
modifier: Modifier = Modifier,
textStyles: TextStyles = KoinJavaComponent.inject(TextStyles::class.java).value,
viewModel: ItemViewModel.GraphicSelect
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
modifier = modifier.weight(weight = 1f) // <-- in alpha06 this gives an error
) {
Image(
asset = vectorResource(id = viewModel.imageId)
)
Text(
modifier = modifier
.padding(
start = dimensionResource(id = R.dimen.spacingL),
top = dimensionResource(id = R.dimen.spacingL),
end = dimensionResource(id = R.dimen.spacingL),
bottom = dimensionResource(id = R.dimen.spacingS)
),
text = viewModel.caption,
style = textStyles.TitleSmall.merge(TextStyle(color = colorResource(id = R.color.tint_secondary)))
)
RadioButton(
selected = viewModel.selected,
onClick = viewModel.action,
colors = RadioButtonConstants.defaultColors(
selectedColor = colorResource(R.color.brand),
unselectedColor = colorResource(R.color.tint_secondary)
)
)
}
}
@Composable
有趣的设置图形选择组合视图(
修饰符:修饰符=修饰符,
textStyles:textStyles=KoinJavaComponent.inject(textStyles::class.java).value,
viewModel:ItemViewModel.GraphicsSelect
) {
纵队(
水平对齐=对齐。中心水平,
垂直布置=中心布置
修改器=修改器。重量(重量=1f)//
val switchers=items.filter{it.viewModel是ItemViewModel.Switcher}
val graphicSelects=items.filter{it.viewModel是ItemViewModel.GraphicSelect}
if(switchers.isNotEmpty()){
val自动开关=开关[0]
设置SwitcherComposeView(viewModel=autoSwitcher.viewModel作为ItemViewModel.Switcher)
}
划船(
修饰语=修饰语
.padding(水平=维度资源(id=R.dimen.间距2xL))
) {
如果(graphicSelects.size>1){
val lightSelector=图形选择[0]
val darkSelector=图形选择[1]
划船(
修饰语=修饰语
.边界(
宽度=1.dp,
颜色=颜色资源(R.color.highlight),
形状=圆角拐角形状(
尺寸资源(R.dimen.content\u corner\u radius)
)
)
.padding(垂直=尺寸资源(id=R.dimen.间距2xL))
) {
划船{
设置图形选择组合视图(
viewModel=lightSelector.viewModel作为ItemViewModel.GraphicSelect
)//尝试在行中使用修饰符。权重(1f)
,因此它来自行范围
:
Row {
SettingsGraphicSelectComposeView(
modifier = Modifier.weight(1f),
viewModel = lightSelector.viewModel as ItemViewModel.GraphicSelect
)
Spacer(
modifier = Modifier
.preferredWidth(1.dp)
.preferredHeight(160.dp)
.background(color = colorResource(R.color.highlight))
)
SettingsGraphicSelectComposeView(
modifier = Modifier.weight(1f),
viewModel = darkSelector.viewModel as ItemViewModel.GraphicSelect
)
}
如果此列是行
的子列,则权重
修饰符似乎只能归属于列
,因此团队成员建议将包含设置Graphic SelectComposeView
的行
元素替换为列
,并使权重
属性“合法”如果你愿意的话,因为我们在一个RowScope中,这很有效
我不知道该怎么想,但它是有效的,而且由于Compose现在仍在alpha07上(自从我发表这篇文章以来的新版本),未来情况可能会再次发生变化
变化如下:
Row(
modifier = modifier
.padding(horizontal = dimensionResource(id = R.dimen.spacing2XL))
.border(
width = 1.dp,
color = colorResource(R.color.highlight),
shape = RoundedCornerShape(
dimensionResource(R.dimen.content_corner_radius)
)
)
.fillMaxWidth()
) {
Column(modifier = Modifier.weight(weight = 1f).padding(vertical = dimensionResource(id = R.dimen.spacing2XL))) {
SettingsGraphicSelectComposeView(
modifier = Modifier.align(Alignment.CenterHorizontally),
viewModel = viewModel.light.viewModel as ItemViewModel.GraphicSelect
)
}
Spacer(
modifier = Modifier
.padding(top = dimensionResource(id = R.dimen.spacing2XL))
.preferredWidth(1.dp)
.preferredHeight(160.dp) // TODO: Find a way to make this max AUTO height, not fixed
.background(color = colorResource(R.color.highlight))
)
Column(modifier = Modifier.weight(weight = 1f).padding(vertical = dimensionResource(id = R.dimen.spacing2XL))) {
SettingsGraphicSelectComposeView(
modifier = Modifier.align(Alignment.CenterHorizontally),
viewModel = viewModel.dark.viewModel as ItemViewModel.GraphicSelect
)
}
}
我试过了,但是设置graphicselectcomposeview
在高度
中一直打到页面底部,而不是在宽度
中,正如它应该的那样:-(@Ambran要解决高度问题,您可以尝试将内部“文本”修饰符设置为修饰符。填充(…)
而不是参数修饰符。填充(…)
你是对的。这解决了高度问题。重量问题仍然存在。你认为这是alpha06中的错误吗?
Row(
modifier = modifier
.padding(horizontal = dimensionResource(id = R.dimen.spacing2XL))
.border(
width = 1.dp,
color = colorResource(R.color.highlight),
shape = RoundedCornerShape(
dimensionResource(R.dimen.content_corner_radius)
)
)
.fillMaxWidth()
) {
Column(modifier = Modifier.weight(weight = 1f).padding(vertical = dimensionResource(id = R.dimen.spacing2XL))) {
SettingsGraphicSelectComposeView(
modifier = Modifier.align(Alignment.CenterHorizontally),
viewModel = viewModel.light.viewModel as ItemViewModel.GraphicSelect
)
}
Spacer(
modifier = Modifier
.padding(top = dimensionResource(id = R.dimen.spacing2XL))
.preferredWidth(1.dp)
.preferredHeight(160.dp) // TODO: Find a way to make this max AUTO height, not fixed
.background(color = colorResource(R.color.highlight))
)
Column(modifier = Modifier.weight(weight = 1f).padding(vertical = dimensionResource(id = R.dimen.spacing2XL))) {
SettingsGraphicSelectComposeView(
modifier = Modifier.align(Alignment.CenterHorizontally),
viewModel = viewModel.dark.viewModel as ItemViewModel.GraphicSelect
)
}
}