英文:
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)) {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论