Home > Software engineering >  Is there something like TextView’s center_vertical in Jetpack Compose?
Is there something like TextView’s center_vertical in Jetpack Compose?

Time:09-17

I’d like to find some properties like android:gravity=“center_vertical” in Jetpack Compose without using Column, any ideas?

CodePudding user response:

No, the only way is to use a container. And that is exactly how it should be used in Compose: the container does not add any problems, unnecessary load, etc. No reason why you should avoid them.

To me, using Column is not the most logical solution, because it is designed to vertically arrange several elements.

For a single item, Box is more clear to me:

Box(
    contentAlignment = Alignment.Center,
    modifier = Modifier.fillMaxSize()
) {
    Text("Hello")
}

CodePudding user response:

You can't place a text vertically center without using a container. But you can change the alignment of Text without using a container.

Text(textAlign: TextAlign.Center)

In case of height with wrapContent this will automatically place the text vertically centered.

Other values of TextAlign are Left, Right, Justify, Start and End

  • Related