fillMaxWidth()
fillMaxWidth()(또는 fillMaxHeight)는 현재 컴포저블의 측정 제약(measurement constraints) 내에서 최대 너비(또는 높이)를 채웁니다.
Column(modifier = Modifier.width(300.dp)) {
Box(modifier = Modifier.fillMaxWidth()) // 300.dp를 채움
}
일반적인 레이아웃(Column, Row, Box 등)에서는 잘 작동하지만, LazyRow/LazyColumn의 item 블록 내부에서는 제대로 작동하지 않습니다. 왜냐하면:
- LazyRow는 각 item에게 “너가 원하는 만큼 크기를 가져도 돼”라고 말합니다 (제약이 무한대)
- fillMaxWidth()는 “주어진 제약 내 최대 너비”를 의미하는데, 제약이 무한대면 의미가 없습니다
- 결과적으로 item은 콘텐츠의 고유 크기(intrinsic size)로 측정됩니다
fillParentMaxWidth()
fillParentMaxWidth()(또는 fillParentMaxHeight)는 LazyRow/LazyColumn의 부모 컨테이너 너비(또는 높이)를 기준으로 채웁니다.
LazyRow(
modifier = Modifier.fillMaxWidth(),
contentPadding = PaddingValues(horizontal = 14.dp)
) {
item {
Box(modifier = Modifier.fillParentMaxWidth())
// LazyRow의 viewport 너비(contentPadding 제외)를 채움
// LazyRow가 화면 전체(400.dp)라면,
// viewport는 400.dp - 28.dp = 372.dp이고,
// Box는 372.dp를 채움
}
}
주요 차이점
특징 | fillMax*() | fillParentMax*() |
---|---|---|
적용 대상 | 일반 레이아웃 | LazyRow/LazyColumn의 item |
기준 | 측정 제약(constraints) | 부모 컨테이너의 실제 크기 |
Lazy 리스트에서 | 작동 안 함 | 정상 작동 |
fillParentMaxWidth()는 비율도 지정할 수 있습니다:
LazyRow {
item {
Box(modifier = Modifier.fillParentMaxWidth(0.8f))
// 부모 너비의 80%를 채움
}
}
왜 이런 차이가 생겼나?
LazyRow/LazyColumn은 가상화(virtualization)를 위해 특별한 측정 방식을 사용합니다. 화면에 보이는 item만 실제로 컴포즈하고 측정하기 때문에, 일반적인 제약 전파 방식으로는 부모 크기를 알 수 없습니다. 그래서
fillParentMaxWidth()/fillParentMaxHeight()라는 별도의 modifier가 필요한 것입니다.
0개의 댓글