如何在Jetpack Compose中将Card置于地图视图底部?
让Box布局中的Card靠近底部显示的两种方法
你当前的问题是Box设置了contentAlignment = Alignment.Center,导致Card默认居中显示。要让Card靠底部,有两种简单的解决方式:
方法一:修改Box的全局对齐方式
把Box的contentAlignment改成Alignment.BottomCenter,这样所有未单独指定对齐的子组件都会底部居中。因为MapScreen()一般会占满整个Box的空间,所以这个设置不会影响地图的显示,只会让Card定位到底部:
Box( contentAlignment = Alignment.BottomCenter ) { MapScreen() Card( modifier = Modifier .width(380.dp) .height(350.dp), elevation = 10.dp, shape = RoundedCornerShape(15.dp) ) { Column( Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally ) { Spacer(modifier = Modifier.height(10.dp)) Recomendationm(indicatorValue = value) Text("Here") } } }
方法二:给Card单独设置对齐修饰符
如果不想改变Box的全局对齐规则,只需要给Card的Modifier添加.align(Alignment.BottomCenter),单独指定它的对齐位置,这样其他组件依然遵循Box的contentAlignment:
Box( contentAlignment = Alignment.Center ) { MapScreen() Card( modifier = Modifier .width(380.dp) .height(350.dp) .align(Alignment.BottomCenter), // 新增这一行指定对齐 elevation = 10.dp, shape = RoundedCornerShape(15.dp) ) { Column( Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally ) { Spacer(modifier = Modifier.height(10.dp)) Recomendationm(indicatorValue = value) Text("Here") } } }
如果需要Card和底部保留一点间距,可以在Card的Modifier里追加.padding(bottom = 16.dp)(数值按需调整),比如:
Modifier .width(380.dp) .height(350.dp) .align(Alignment.BottomCenter) .padding(bottom = 16.dp)
内容的提问来源于stack exchange,提问作者user9408023
相关产品推荐
相关产品推荐

