如何在Jetpack Compose中将元素居中放置在列中

时间:2020-03-01 20:41:56

标签: android android-layout kotlin android-jetpack android-jetpack-compose

如何将元素居中嵌入在行内的第一列中:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            MaterialTheme {
                Row {
                    Column(modifier = LayoutPadding(top = 16.dp)) {
                        Text(text = "Centered ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
                    }
                    Column {
                        Text(text = "Line One ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
                        Text(text = "Line Two ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
                    }
                 }
             }
         }
     }
}

在此示例中,我对填充进行了硬编码,但是无法弄清楚如何将元素居中放置。如果没有这样的选择,如何获得整个Row的高度?

2 个答案:

答案 0 :(得分:0)

容器(alignment = Alignment.TopCenter)或Center {}将为您提供帮助。

尝试一下,

    MaterialTheme {
        Row {
            Container(width = 200.dp) {
                Center {
                    Column() {
                        Text(text = "Centered ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
                    }
                }
            }
            Column {
                Text(text = "Line One ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
                Text(text = "Line Two ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
            }
        }
    }

    MaterialTheme {
        Row {
            Container(width = 200.dp, alignment = Alignment.TopCenter) {
                    Column() {
                        Text(text = "Centered ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
                    }
            }
            Column {
                Text(text = "Line One ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
                Text(text = "Line Two ", style = TextStyle(fontSize = 30.sp, fontWeight = FontWeight.Bold))
            }
        }
    }

答案 1 :(得分:0)

\n中,使用1.0.0-alpha03中的Modifier.align

Column

enter image description here