android - 在 Jetpack Compose 中将高度设置为 Row 相当于 xml 中的wrap_content

标签 android kotlin android-layout android-jetpack-compose android-jetpack-compose-row

我的这一行有两个按钮和一个分隔线。
我设置了权重,以便按钮可以具有相同的宽度。

Row(modifier = Modifier
    .fillMaxWidth()
    .padding(horizontal = 5.dp)
    .height(300.dp)
) {
    Button(
        stringResource(id = R.string.ha_topos_button_description),
        Modifier.weight(1f)
    )
    Divider(whichType = "vertical")
    Button(
        stringResource(id = R.string.ha_gyms_button),
        Modifier.weight(1f)
    )
}

如果我没有将 .height(300.dp) 属性设置为 Row,它就会消失。我不想设置任何硬编码高度,我只是希望该行具有其“wrap_content”高度。
我怎样才能在 Compose 上做到这一点???

最佳答案

使用wrapContentHeight使行换行到内容的高度:

Row(modifier = Modifier
    .fillMaxWidth()
    .padding(horizontal = 5.dp)
    .wrapContentHeight()
) {
    Button(
        stringResource(id = R.string.ha_topos_button_description),
        Modifier.weight(1f)
    )
    Divider(whichType = "vertical")
    Button(
        stringResource(id = R.string.ha_gyms_button),
        Modifier.weight(1f)
    )
}

关于android - 在 Jetpack Compose 中将高度设置为 Row 相当于 xml 中的wrap_content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69901020/

相关文章:

spring - Java Stream with::Kotlin 新手

java - Android fragment 错误 : No view found for id 0x7f08009e (com. example.myapplication:id/preferenceFragment) fragment

Android创建计算器应用程序

c# - 使用 Xamarin Forms 在 ContentResolver.Insert() 上获取编译错误以创建事件

android - 删除解析文件

java - 如何设置图片只上传jpg和png文件?

android - 在抽屉导航中单击项目时在 viewpager 中切换 fragment

java - 扫描字符串直到找到字符串中的特定字符

kotlin - 为什么生成的 getter 方法中有美元符号?

java - 如何在 Android 中使用字符串计算数学表达式?