Reputation: 587
I have a Box layout and I want to layout child views relative to the size of the parent box. This is achievable in SwiftUI using Geometry Reader. How can I achieve something similar in Jetpack Compose ?
Upvotes: 4
Views: 7752
Reputation: 363439
You can use BoxWithConstraints
instead of a Box
.
You can work with the measurement constraints available in the scope of the content lambda.
Something like:
BoxWithConstraints {
Box(Modifier.width(maxWidth*0.7f).height(30.dp).background(Color.Blue))
Box(Modifier.width(maxWidth*0.3f).height(30.dp).background(Color.Yellow))
}
Otherwise you can use the Box
and fillMaxWidth
and fillMaxHeight
modifiers with a fraction.
Box(){
Box(Modifier.fillMaxWidth(0.7f).height(30.dp).background(Color.Blue))
Box(Modifier.fillMaxWidth(0.3f).height(30.dp).background(Color.Yellow))
}
Upvotes: 11