[英]What is the equivalent of Expanded widget of Flutter in Jetpack Compose?
I am pretty much new to Jetpack Compose
and I want to create a simple business card app like this:我对
Jetpack Compose
非常陌生,我想创建一个简单的名片应用程序,如下所示:
The way I want to achieve this layout is to create two main Columns
, one for the upper part (logo with name and title) and one for the lower part of the screen (contact details).我想要实现这种布局的方式是创建两个主要的
Columns
,一个用于上部(带有名称和标题的徽标),一个用于屏幕下部(联系方式)。 I want the first Column
to take all the remaining space of the screen, so the lower Column
goes to the end of the page.我希望第一
Column
占据屏幕的所有剩余空间,因此较低的Column
转到页面的末尾。 In Flutter
I would place both of the Columns
in a parent Column
and wrap the first Column
with Expanded
, and it would do the job.在
Flutter
,我将两Columns
都放在父Column
中,并用Expanded
包裹第一Column
,它就可以完成这项工作。 So how to achieve the same behavior in Jetpack Compose
?那么如何在
Jetpack Compose
中实现相同的行为呢? The code is as follows:代码如下:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
BusinessCardTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
BusinessCard()
}
}
}
}
}
@Composable
fun LogoNameTitle() {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Image(
painter = painterResource(id = R.drawable.android_logo),
contentDescription = "",
)
Text("Jennifer Doe", color = Color.White)
Text("Android Developer Extraordinaire", color = Color.Green)
}
}
@Composable
fun ContactInformation() {
Column() {
Row() {
Icon(imageVector = Icons.Rounded.Phone, contentDescription = "", tint = Color.Green)
Text("+11 (123) 444 555 666", color = Color.White)
}
Row() {
Icon(imageVector = Icons.Rounded.Share, contentDescription = "", tint = Color.Green)
Text("@AndroidDev", color = Color.White)
}
Row() {
Icon(imageVector = Icons.Rounded.Email, contentDescription = "", tint = Color.Green)
Text("jen.doe@android.com", color = Color.White)
}
}
}
@Composable
fun BusinessCard() {
Surface(color = Color.DarkGray) {
Column(
modifier = Modifier.padding(16.dp)
) {
LogoNameTitle()
ContactInformation()
}
}
}
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun BusinessCardPreview() {
BusinessCardTheme {
BusinessCard()
}
}
And the current output is:而当前的output为:
Use as parent container a Column
and just apply the weight(1f)
modifier to the 1st nested Column
(name and title).使用
Column
作为父容器,只需将weight(1f)
修饰符应用于第一个嵌套Column
(名称和标题)。
Something like:就像是:
Column(modifier = Modifier.fillMaxSize()){
Column(Modifier.fillMaxWidth().weight(1f)){
//Logo and title
}
Column(){
//Contact info
Text("First Name")
Text("Surname")
Text("Other info")
}
}
Pls note that the weight
modifier requires a ColumnScope
.请注意,
weight
修饰符需要ColumnScope
。 In your case use the modifier as parameter:在您的情况下,使用修饰符作为参数:
@Composable
fun LogoNameTitle(modifier: Modifier = Modifier) {
Column(
modifier = modifier,
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
//...
}
}
@Composable
fun BusinessCard() {
Surface(color = Color.DarkGray,modifier = Modifier.fillMaxSize()) {
Column(
modifier = Modifier.fillMaxSize().padding(16.dp)
) {
LogoNameTitle(modifier=Modifier.weight(1f))
ContactInformation()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.