1

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

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))
                    }
                 }
             }
         }
     }
}

在此示例中,我对填充进行了硬编码,但无法弄清楚如何使元素居中。如果没有这样的选项,我怎样才能得到整行的高度?

4

2 回答 2

4

使用你1.0.0Modifier.alignColumn

Row (){
    Column( modifier = Modifier.align(Alignment.CenterVertically)){
        Text(text = "Centered ", style = textStyle)
    }
    Column {
        Text(text = "Line One ", style = textStyle)
        Text(text = "Line Two ", style = textStyle)
    }
}

在此处输入图像描述

或者您可以verticalAlignment = Alignment.CenterVerticallyRow元素中使用:

Row (verticalAlignment = Alignment.CenterVertically){
    Column(){
        Text(text = "Centered ", style = textStyle)
    }
    Column() {
        Text(text = "Line One ", style = textStyle)
        Text(text = "Line Two ", style = textStyle)
    }
}
于 2020-09-02T10:27:58.560 回答
0

Container(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))
            }
        }
    }
于 2020-03-03T07:09:01.263 回答