Is it possible to specify the padding amount as a ...
# compose
j
Is it possible to specify the padding amount as a percentage of a composable’s width/height?
z
as a percentage of the inner or outer composable?
j
Inner
(I mean, the same composable I apply the padding modifier to)
O gosh. I think you got me. I don't even know what I'm talking about. Perhaps it's relative to the outer composable.
Only way I could do it was to wrap it into a box:
Copy code
Box(
      modifier = Modifier
        .aspectRatio(1f)
        .fillMaxSize(),
      contentAlignment = Alignment.Center
    ) {
      MyComposable(
        modifier = Modifier
          .fillMaxSize(0.75f)
      )
    }
z
Hm, i don’t think you need a box there.
Copy code
MyComposable(
  Modifier
    .aspectRatio(1f)
    .fillMaxSize()
    .wrapContentSize(align = Alignment.Center)
    .fillMaxSize(0.75f)
)
j
Thanks a lot, this also works, but I don’t understand why: I’ve tried to interpret `wrapContentSize`’s docstring but couldn’t actually relate it to what’s happening in this specific case. If you could explain it’d be greatly appreciated. Other question to let me learn more about the correct terminologies: would you call this padding as a percentage of the inner or the outer composable? Why?
z
It might actually work without wrap content size, but then the alignment would depend on however the parent layout decides to place children that don't take up the whole space (and the simplest thing to do is to just place them start-left).
It might also not need that first fillMaxSize, I'd have to try it to know.
This chain does: 1. First start with all the available space. 2. Then, because i’m about to put something smaller inside, use wrapWithContent to allow the smaller thing to be whatever size it wants and to control its alignment inside the bigger thing. 3. Lastly, take up 3/4 of the available space.
j
It might also not need that first fillMaxSize, I’d have to try it to know.
Just tried and, indeed, it also works without it!
z
Yea i think
aspectRatio
already passes tight constraints down.
And even if it didn’t it probably doesn’t matter since
wrapContentSize
should pass the max constraints down untouched.
j
It might actually work without wrap content size, but then the alignment would depend on however the parent layout decides to place children that don’t take up the whole space (and the simplest thing to do is to just place them start-left).
Precisely, without
wrapContentSize
the inner composable ended up with a different alignment.
1235 Views