【发布时间】:2021-09-23 06:20:40
【问题描述】:
如何创建依赖于其他一些修饰符值的新修饰符?举例来说,我想让子组件的高度为从父组件传入的高度的一半加上一些常数(因此无法使用 fillMaxHeight(fraction: Float))。
@Composable
fun View() {
MyComposable(modifier = Modifier.size(40.dp))
}
@Composable
fun MyComposable(
modifier: Modifier // Assuming this modifier contains a size
) {
Box(modifier) { // Setting the the size from the passed in modifier
val childHeightModifier = Modifier.height(???) // <-- Wanted to be some specific value depending on parent size
Box(childHeightModifier) {
...
}
}
}
【问题讨论】:
-
另一种看待这个问题的方式是询问;如何制作只对传入修饰符的某些特定值感兴趣的新修饰符?假设传入的修饰符具有 .background(..).size(40.dp) 并且我只想将此修饰符的大小值传递给孩子。
标签: android android-jetpack-compose