gpt4 book ai didi

android - Jetpack 撰写 : Row with all items same height

转载 作者:行者123 更新时间:2023-12-04 23:42:38 25 4
gpt4 key购买 nike

我正在尝试在 Compose 中实现一个布局,其中水平可滚动的项目 Row应该都具有相同的高度,因此较小的项目应调整为该行中最大项目的大小。我知道intrinsic size但我就是无法让它工作。此外,我不想为 Row 分配固定高度,因为 Row 的高度也应该是其最大的可组合子项的高度。
这是简化的布局

@Composable
fun Screen(
modifier: Modifier = Modifier,
) {
Row(
modifier = modifier
.height(IntrinsicSize.Min)
.horizontalScroll(state = rememberScrollState()),
horizontalArrangement = Arrangement.spacedBy(10.dp),
) {
Item(
text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
"eirmod tempor invidunt ut labore et dolore magna aliquyam"
)

Item(
text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
"voluptua. At"
)

Item(
text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam"
)
}
}

@Composable
private fun Item(
modifier: Modifier = Modifier,
text: String,
) {
Column(
modifier = modifier.width(200.dp),
horizontalAlignment = Alignment.End,
verticalArrangement = Arrangement.SpaceBetween
) {
Column {
Text("Some static text")

// Dynamic text
Text(
text,
modifier = Modifier.padding(top = 5.dp)
)
}

// The space between these two composables should be flexible,
// hence the outer column with Arrangement.SpaceBetween

Button(
modifier = Modifier.padding(top = 20.dp),
onClick = {}
) {
Text("Button")
}
}
}
这是结果
what I get
但我真正想要的是
what I want
当我申请 fillMaxHeight()Item ,项目占据整个高度,所有按钮都对齐到屏幕底部。
Jetpack Compose 版本:1.1.0

最佳答案

使用 SubComposeLayout 可以实现这样的功能来设置每个元素的高度这使您可以根据新指标(例如具有最大宽度或高度的同级)重新测量您的可组合项。
您可以查看 SubComposeLayout here 的描述,或者我的答案是列具有相等的宽度 here .

@Composable
fun SubcomposeRow(
modifier: Modifier = Modifier,
content: @Composable () -> Unit = {},
) {

SubcomposeLayout(modifier = modifier) { constraints ->

var recompositionIndex = 0

var placeables: List<Placeable> = subcompose(recompositionIndex++, content).map {
it.measure(constraints)
}

placeables.forEachIndexed() { index: Int, placeable: Placeable ->
println("Index: $index, placeable width: ${placeable.width}, height: ${placeable.height}")
}

var rowSize =
placeables.fold(IntSize.Zero) { currentMax: IntSize, placeable: Placeable ->
IntSize(
width = currentMax.width + placeable.width,
height = maxOf(currentMax.height, placeable.height)
)
}


// Remeasure every element using height of longest item as minHeight of Constraint
if (!placeables.isNullOrEmpty() && placeables.size > 1) {
placeables = subcompose(recompositionIndex, content).map { measurable: Measurable ->
measurable.measure(
Constraints(
minHeight = rowSize.height,
maxHeight = constraints.maxHeight
)
)
}

rowSize =
placeables.fold(IntSize.Zero) { currentMax: IntSize, placeable: Placeable ->
IntSize(
width = currentMax.width + placeable.width,
height = maxOf(currentMax.height, placeable.height)
)
}
}

layout(rowSize.width, rowSize.height) {
var xPos = 0
placeables.forEach { placeable: Placeable ->
placeable.placeRelative(xPos, 0)
xPos += placeable.width
}

}
}
}
第二次测量中的约束很重要,因为我们希望每个组合都具有最大高度
                Constraints(
minHeight = rowSize.height,
maxHeight = constraints.maxHeight
)
用法
@Composable
fun Screen(
modifier: Modifier = Modifier,
) {
SubcomposeRow(
modifier = modifier
.background(Color.LightGray)
.horizontalScroll(state = rememberScrollState()),
) {
Item(
text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
"eirmod tempor invidunt ut labore et dolore magna aliquyam"
)

Item(
text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy " +
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam " +
"voluptua. At"
)

Item(
text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam"
)
}
}

@Composable
private fun Item(
modifier: Modifier = Modifier,
text: String,
) {
Column(
modifier = modifier
.width(200.dp)
.background(Color.Red),
horizontalAlignment = Alignment.End,
verticalArrangement = Arrangement.SpaceBetween
) {
Column(modifier = Modifier.background(Color.Yellow)) {
Text("Some static text")

// Dynamic text
Text(
text,
modifier = Modifier.padding(top = 5.dp)
)
}

// The space between these two composables should be flexible,
// hence the outer column with Arrangement.SpaceBetween

Button(
modifier = Modifier
.padding(top = 20.dp),
onClick = {}
) {
Text("Button")
}
}
}
结果
enter image description here

关于android - Jetpack 撰写 : Row with all items same height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71080209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com