kotlin 如何在jetpack compose中将组件堆叠在一起?

hjzp0vay  于 7个月前  发布在  Kotlin
关注(0)|答案(3)|浏览(113)

我想创建一个 Jmeter 板,我想在顶部使用这种布局。如何在jetpack compose中实现这样的布局?top dashboard layouttop dashboard layout 2

qhhrdooz

qhhrdooz1#

您可以使用Box或BoxWithConstraints来实现这种外观。这里的关键是将中心的Box与底部对齐,并添加底部填充,其大小为底部Composable的高度与底部Composable的半高之和,因为底部和顶部的Composable的高度不相等。

@Composable
private fun MyComposable(modifier: Modifier = Modifier) {
    BoxWithConstraints(modifier = modifier.fillMaxWidth(1f)) {
        val maxHeight = this.maxHeight

        val topHeight: Dp = maxHeight * 2 / 3
        val bottomHeight: Dp = maxHeight / 3

        val centerHeight = 100.dp

        val centerPaddingBottom = bottomHeight - centerHeight / 2

        Top(
            modifier = Modifier
                .fillMaxWidth()
                .align(Alignment.TopCenter)
                .height(topHeight)
        )

        Bottom(
            modifier = Modifier
                .fillMaxWidth()
                .align(Alignment.BottomCenter)
                .height(bottomHeight)
        )

        Center(
            modifier = Modifier
                .padding(start = 10.dp, end = 10.dp, bottom = centerPaddingBottom)
                .fillMaxWidth()
                .height(centerHeight)
                .align(Alignment.BottomCenter)
        )
    }
}

@Composable
private fun Top(modifier: Modifier) {
    Column(modifier.background(Blue400)) {

    }
}

@Composable
private fun Bottom(modifier: Modifier) {
    Column(modifier.background(Color.White)) {

    }
}

@Composable
fun Center(modifier: Modifier) {
    Column(modifier.background(Color.Red, shape = RoundedCornerShape(10.dp))) {

    }
}

字符串
结果


的数据

8ftvxx2r

8ftvxx2r2#

一般来说,你可以使用一个盒子。
参见此处的示例https://foso.github.io/Jetpack-Compose-Playground/layout/box/

Box(Modifier.fillMaxSize()) {
        Text("This text is drawn first", modifier = Modifier.align(Alignment.TopCenter))
        Box(
            Modifier.align(Alignment.TopCenter).fillMaxHeight().width(
                50.dp
            ).background( Color.Blue)
        )
        Text("This text is drawn last", modifier = Modifier.align(Alignment.Center))
        FloatingActionButton(
            modifier = Modifier.align(Alignment.BottomEnd).padding(12.dp),
            onClick = {}
        ) {
            Text("+")
        }
    }

字符串

vu8f3i0k

vu8f3i0k3#

如果你想创建一个包含内容列表的堆栈视图,你可以使用下面的代码来实现,这里的Arrangement.spacedBy((-50).dp)用于提供堆栈效果。

@Composable
fun CardsStackView(cards: List<CardUiModel> = getMockedCards()) {
    LazyColumn(
        verticalArrangement = Arrangement.spacedBy((-50).dp),
        modifier = Modifier
            .padding(top = 50.dp, end = 10.dp, start = 10.dp)
    ) {
        itemsIndexed(cards) { index, card ->
            CardView(index, card)
        }
    }
}

@Composable
fun CardView(index: Int, cardUiModel: CardUiModel) {
    Card(
        shape = RoundedCornerShape(8.dp),
        modifier = Modifier
            .height(80.dp)
            .width(260.dp)
    ) {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = getCardColor(index)
        ) {
            Box(contentAlignment = Center) {
                Text(text = cardUiModel.name)
            }
        }
    }
}

private fun getMockedCards() = listOf(
    CardUiModel("Visa"),
    CardUiModel("Amex"),
    CardUiModel("Rupay"),
    CardUiModel("Master Card")
)

private fun getCardColor(index: Int) =
    when (index) {
        0 -> Color.Blue
        1 -> Color.Magenta
        2 -> Color.Cyan
        3 -> Color.Red
        else -> Color.Yellow
    }

data class CardUiModel(val name: String)

@Preview(showBackground = true, device = Devices.PIXEL_XL)
@Composable
fun CardsStackViewPreview() {
    MaterialTheme {
        CardsStackView()
    }
}

字符串
它看起来像下面的图像
x1c 0d1x的数据

相关问题