shape = RoundedCornerShape(8.dp)
속성을 사용해 모서리를 얼마나 둥글게 만들지 설정할 수 있음Card(modifier = modifier) {
Column {
Image(
painter = painterResource(affirmation.imageResourceId),
contentDescription = stringResource(affirmation.stringResourceId),
modifier = Modifier
.fillMaxWidth()
.height(194.dp),
contentScale = ContentScale.Crop
)
Text(
text = LocalContext.current.getString(affirmation.stringResourceId),
modifier = Modifier.padding(16.dp),
style = MaterialTheme.typography.headlineSmall // 텍스트 테마 설정
)
}
}
Column
과의 차이점 : Compose가 한 번에 모두 로드하므로 표시할 항목이 적은 경우 Column
을 사용해야 한다는 것items()
메서드는 LazyColumn
에 항목을 추가LazyColumn(modifier = modifier) {
items(affirmationList) { affirmation ->
AffirmationCard(
affirmation = affirmation,
modifier = Modifier.padding(8.dp)
)
}
}
@Composable
fun AffirmationsApp() {
AffirmationList(
affirmationList = Datasource().loadAffirmations(),
)
}
@Composable
fun AffirmationList(affirmationList: List<Affirmation>, modifier: Modifier = Modifier) {
LazyColumn(modifier = modifier) {
items(affirmationList) { affirmation ->
AffirmationCard(
affirmation = affirmation,
modifier = Modifier.padding(8.dp)
)
}
}
}
// 모든 컴포저블에 수정자를 전달하고 기본값을 설정하는 것이 좋음
@Composable
fun AffirmationCard(affirmation: Affirmation, modifier: Modifier = Modifier) {
Card(modifier = modifier) {
Column {
Image(
painter = painterResource(affirmation.imageResourceId),
contentDescription = stringResource(affirmation.stringResourceId),
modifier = Modifier
.fillMaxWidth()
.height(194.dp),
contentScale = ContentScale.Crop
)
Text(
text = LocalContext.current.getString(affirmation.stringResourceId),
modifier = Modifier.padding(16.dp),
style = MaterialTheme.typography.headlineSmall // 텍스트 테마 설정
)
}
}
}
위에서 배웠던 것을 바탕으로 그리드 레이아웃을 스크롤 하는 앱을 만들어 볼 것이다!
verticalArrangement
, horizontalArrangement
을 사용해 양옆과 위, 아래에 8dp만큼의 간격을 줌LazyVerticalGrid(
columns = GridCells.Fixed(2), // 2개씩 배치
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = modifier
) {
items(DataSource.topics) { topic ->
TopicCard(topic)
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CoursesTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
TopicGrid(
modifier = Modifier.padding(8.dp) // 화면 상하좌우에 8dp만큼의 padding 설정
)
}
}
}
}
}
@Composable
fun TopicGrid(modifier: Modifier = Modifier) {
LazyVerticalGrid(
columns = GridCells.Fixed(2), // 2개씩 배치
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = modifier
) {
items(DataSource.topics) { topic ->
TopicCard(topic)
}
}
}
@Composable
fun TopicCard(topic: Topic, modifier: Modifier = Modifier) {
Card {
Row {
Image(painter = painterResource(topic.imageRes),
contentDescription = stringResource(topic.name),
modifier = Modifier
.size(width = 68.dp, height = 68.dp)
.aspectRatio(1f),
contentScale = ContentScale.Crop
)
Column {
Text(
text = stringResource(id = topic.name),
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.padding(
start = 16.dp,
top = 16.dp,
end = 16.dp,
bottom = 8.dp
)
)
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
painter = painterResource(R.drawable.ic_grain),
contentDescription = null,
modifier = Modifier
.padding(start = 16.dp)
)
Text(
text = topic.availableCourses.toString(),
style = MaterialTheme.typography.labelMedium,
modifier = Modifier.padding(start = 16.dp)
)
}
}
}
}
}