Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Compose, Align items in row

I'm using the brand new Kotlin Compose for my view, I have a row with 2 items, how can I make them center-vertical-like?

    Row(
            modifier = Spacing(20.dp)
    ) {
        Text(text = "Hello $name!")
        Spacing(10.dp)
        Padding(padding = 25.dp) {
            Button(text = "Click", onClick = { /*do something*/ })

        }
    }

enter image description here

Note that without padding they are not aligned too.

like image 779
Alireza Akbari Avatar asked Dec 08 '19 06:12

Alireza Akbari


2 Answers

There's verticalAlignment argument in Row. You can also configure an item alignment individually using align modifier which overrides the row's verticalAlignment. For example:

@Composable
fun RowAlignmentExample() {
    Row(verticalAlignment = Alignment.CenterVertically) {
        SizeTile(14)
        SizeTile(18)
        SizeTile(22)
        SizeTile(26)
        SizeTile(10, modifier = Modifier.align(Alignment.Top))
        SizeTile(10, modifier = Modifier.align(Alignment.Bottom))
    }
}

@Composable
fun SizeTile(fontSize: Int, modifier: Modifier = Modifier) {
    Text(
        text = fontSize.toString(),
        fontSize = fontSize.sp,
        modifier = modifier
            .padding(2.dp)
            .background(Color.White, RoundedCornerShape(4.dp))
            .padding(2.dp)
    )
}

enter image description here

like image 88
Valeriy Katkov Avatar answered Nov 20 '22 17:11

Valeriy Katkov


On compose version 1.0, this is a solution...

Row(
    modifier = Modifier.padding(20.dp),
    verticalAlignment = Alignment.CenterVertically
) {
    Text(text = "Hello Android!",
        modifier = Modifier.padding(end = 10.dp))
    Button(text = { Text("Click") },
        onClick = { /*do something*/ },
        modifier = Modifier.padding(25.dp)
    )
}
like image 7
nglauber Avatar answered Nov 20 '22 17:11

nglauber