在Jetpack Compose中将列对齐到底部中心

huangapple go评论66阅读模式
英文:

Align Column to Bottom Center in Jetpack Compose

问题

我刚刚开始使用Jetpack Compose,所以对我来说一切都还很新。然而,我在某个帖子上看到,为了将列放在屏幕底部,我可以使用.weight(1f).align(Alignment.BottomCenter)。然而,这两者似乎都不起作用。

在使用.weight时,我收到以下导入错误消息:

无法访问'ColumnScopeInstance':它在'androidx.compose.foundation.layout'中是内部的

而在使用.align时,我收到以下错误消息:

无法访问'BoxScopeInstance':它在'androidx.compose.foundation.layout'中是内部的

我的代码目前如下所示:

@Composable
fun SpeedBar() {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier
            .clip(RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp))
            .background(mycolor)
            .fillMaxWidth()
            .weight(1f) // 这不起作用
            .align(Alignment.BottomCenter) // 这也不起作用
            .padding(15.dp)
    ) {
        Image(
            painter = painterResource(id = R.drawable.button_image),
            contentDescription = "Button1",
            modifier = Modifier
                .size(width = 150.dp, height = 150.dp)
                .clip(RoundedCornerShape(16.dp))
                .clickable {

                }
        )

        Button(
            colors = ButtonDefaults.buttonColors(Color.Green),
            onClick = { /*TODO*/ },
            modifier = Modifier
                .padding(8.dp)
        ) {
            Text(
                text = "Placeholder",
                color = Color.White,
                fontSize = 24.sp
            )
        }
    }
}

Android Studio给我提供的导入是:

import androidx.compose.foundation.layout.ColumnScopeInstance.weight

如果需要进一步帮助或解释,请告诉我。

英文:

So I've just started using Jetpack Compose so everything is pretty new to me.
However I saw on some post that in order to put a column on the bottom of the screen I could use: .weight(1f) or .align(Alignment.BottomCenter). However neither of these seem to work.

I get this message in the import (.weight):

`Cannot access 'ColumnScopeInstance': it is internal in 'androidx.compose.foundation.layout`

And this message on (.align):

Cannot access 'BoxScopeInstance': it is internal in 'androidx.compose.foundation.layout'

How my code currently looks:

@Composable
fun SpeedBar() {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier
            .clip(RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp))
            .background(mycolor)
            .fillMaxWidth()
            .weight(1f) // This doesn't work
            .align(Alignment.BottomCenter) // This also doesn't work
            .padding(15.dp)
    ) {
        Image(
            painter = painterResource(id = R.drawable.button_image),
            contentDescription = "Button1",
            modifier = Modifier
                .size(width = 150.dp, height = 150.dp)
                .clip(RoundedCornerShape(16.dp))
                .clickable {

                }
        )

        Button(
            colors = ButtonDefaults.buttonColors(Color.Green),
            onClick = { /*TODO*/ },
            modifier = Modifier
                .padding(8.dp)
        ) {
            Text(
                text = "Placeholder",
                color = Color.White,
                fontSize = 24.sp
            )
        }
    }
}

The import Android Studio gives me:

import androidx.compose.foundation.layout.ColumnScopeInstance.weight

答案1

得分: 1

正如 @ayman-ait 提到的,如果不将元素放在 Box 中,就不能使用 align 修饰符。您可以像这样做:

创建一个最大尺寸的 Box,并将列放入其中。

Box(modifier = Modifier.fillMaxSize()) {
    // 在这里添加您的列(带有 align 修饰符)
    Column(modifier = Modifier.align(Alignment.BottomCenter)) {

    }
}
英文:

As @ayman-ait mentioned, you can't use align modifier without keeping the element in a Box. You can do something like this:

Create a Box with max size and put the column in it.

Box(modifier = Modifier.fillMaxSize()) {
    // add your column here (with align modifier)
    Column(modifier = Modifier.align(Alignment.BottomCenter)) {

    }
}

huangapple
  • 本文由 发表于 2023年7月6日 17:32:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定