Jetpack Compose中两列之间的垂直分隔线

huangapple go评论76阅读模式
英文:

Vertical Divider between Two Columns in Jetpack Compose

问题

无法似乎在这种情况下使这个垂直分隔符工作:

<-- begin snippet: js hide: false console: true babel: false -->

<-- language: lang-js -->
Row(
    modifier = modifier
        .fillMaxWidth()
        .background(Color.Blue),
    horizontalArrangement = Arrangement.SpaceEvenly
) {
    Column (
        modifier = modifier
            .fillMaxWidth()
            .weight(1F),

    ) {
        Text(
            modifier = modifier
                .padding(5.dp),
            style = MaterialTheme.typography.bodySmall,
            text = &quot;Testing one two three doo doo doo dood odoodo do dood od o doodood o dood oodo do od odoodo doo od odo od odoodo&quot;,
            overflow = TextOverflow.Ellipsis,
            textAlign = TextAlign.Left
        )
    }
    Box(  //Vertical divider
        modifier
            .fillMaxHeight()
            .width(5.dp)
            .background(color = Color.Yellow)
    )
    Column (
        modifier = modifier
            .fillMaxWidth()
            .weight(1F)
    ) {
        Text(
            modifier = modifier
                .padding(5.dp),
            style = MaterialTheme.typography.bodySmall,
            text = &quot;Testing one two three&quot;,
            overflow = TextOverflow.Ellipsis,
            textAlign = TextAlign.Left
        )
    }
}

<-- end snippet -->

我尝试了以下内容:

<-- begin snippet: js hide: false console: true babel: false -->

<-- language: lang-js -->
Divider(
    color = Color.Black,
    modifier = Modifier
        .height(IntrinsicSize.Max)
)

<-- end snippet -->

但当两边的文本增大时,它仍然不会填充父行的高度:

Jetpack Compose中两列之间的垂直分隔线

英文:

I can't seem to make this vertical divider work in this case:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Row(
                modifier = modifier
                    .fillMaxWidth()
                    .background(Color.Blue),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                Column (
                    modifier = modifier
                        .fillMaxWidth()
                        .weight(1F),

                ) {
                    Text(
                        modifier = modifier
                            .padding(5.dp),
                        style = MaterialTheme.typography.bodySmall,
                        text = &quot;Testing one two three doo doo doo dood odoodo do dood od o doodood o dood oodo do od odoodo doo od odo od odoodo&quot;,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                }
                Box(  //Vertical divider
                    modifier
                        .fillMaxHeight()
                        .width(5.dp)
                        .background(color = Color.Yellow)
                )
                Column (
                    modifier = modifier
                        .fillMaxWidth()
                        .weight(1F)
                ) {
                    Text(
                        modifier = modifier
                            .padding(5.dp),
                        style = MaterialTheme.typography.bodySmall,
                        text = &quot;Testing one two three&quot;,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                }
            }

<!-- end snippet -->

I have tried the following:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Divider(
                    color = Color.Black,
                    modifier = Modifier
                        .height(IntrinsicSize.Max)
                )

<!-- end snippet -->

But it still doesn't fill the height of the parent row when the text either side increases its size:

Jetpack Compose中两列之间的垂直分隔线

答案1

得分: 2

在 `Row` 上使用 `.height(IntrinsicSize.Min)`,并像这样定义 `Divider`

        Divider(
            color = Color.Yellow,
            modifier = Modifier
                .fillMaxHeight()
                .width(5.dp)
        )

或者像这样在 `Box` 上

        Box(
            modifier = Modifier
                .background(Color.Yellow)
                .fillMaxHeight()
                .width(5.dp)
        )

`Divider` 就是一个带有参数的 `Box`可以轻松地将其用于垂直分隔线因为它是一个常见的UI组件

**完整代码**

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(Color.Blue)
            .height(IntrinsicSize.Min),
        horizontalArrangement = Arrangement.SpaceEvenly
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1F),
        ) {
            Text(
                modifier = Modifier
                    .padding(5.dp),
                style = MaterialTheme.typography.bodySmall,
                text = &quot;Testing one two three doo doo doo dood odoodo do dood od o doodood o dood oodo do od odoodo doo od odo od odoodo&quot;,
                overflow = TextOverflow.Ellipsis,
                textAlign = TextAlign.Left
            )
        }
        Divider(
            color = Color.Yellow,
            modifier = Modifier
                .fillMaxHeight()
                .width(5.dp)
        )
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1F)
        ) {
            Text(
                modifier = Modifier
                    .padding(5.dp),
                style = MaterialTheme.typography.bodySmall,
                text = &quot;Testing one two three&quot;,
                overflow = TextOverflow.Ellipsis,
                textAlign = TextAlign.Left
            )
        }
    }

**截图**

[![enter image description here][1]][1]

[Android 文档参考](https://developer.android.com/jetpack/compose/layouts/intrinsic-measurements)

  [1]: https://i.stack.imgur.com/LpZhG.png
英文:

Use .height(IntrinsicSize.Min) on the Row and define Divider like this,

    Divider(
        color = Color.Yellow,
        modifier = Modifier
            .fillMaxHeight()
            .width(5.dp)
    )

or Box like this,

    Box(
        modifier = Modifier
            .background(Color.Yellow)
            .fillMaxHeight()
            .width(5.dp)
    )

A Divider is a just Box with parameters to use it easily for Vertical divider use cases as it is a common UI component.

Complete Code

Row(
    modifier = Modifier
        .fillMaxWidth()
        .background(Color.Blue)
        .height(IntrinsicSize.Min),
    horizontalArrangement = Arrangement.SpaceEvenly
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .weight(1F),
    ) {
        Text(
            modifier = Modifier
                .padding(5.dp),
            style = MaterialTheme.typography.bodySmall,
            text = &quot;Testing one two three doo doo doo dood odoodo do dood od o doodood o dood oodo do od odoodo doo od odo od odoodo&quot;,
            overflow = TextOverflow.Ellipsis,
            textAlign = TextAlign.Left
        )
    }
    Divider(
        color = Color.Yellow,
        modifier = Modifier
            .fillMaxHeight()
            .width(5.dp)
    )
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .weight(1F)
    ) {
        Text(
            modifier = Modifier
                .padding(5.dp),
            style = MaterialTheme.typography.bodySmall,
            text = &quot;Testing one two three&quot;,
            overflow = TextOverflow.Ellipsis,
            textAlign = TextAlign.Left
        )
    }
}

Screenshot

Jetpack Compose中两列之间的垂直分隔线

Android Docs for Reference

huangapple
  • 本文由 发表于 2023年7月23日 17:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定