This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
DefaultTheme { | |
// A surface container using the 'background' color from the theme | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = MaterialTheme.colorScheme.background | |
) { | |
LazyVerticalGridExample() | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun LazyVerticalGridExample() { | |
val numbers = (1..100).toList() | |
LazyVerticalGrid( | |
columns = GridCells.Fixed(2), // 열의 개수를 2로 설정 | |
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp) | |
) { | |
items(numbers) { number -> | |
// 각 항목을 그리드 아이템으로 표시 | |
GridItem(number) | |
} | |
} | |
} | |
@Composable | |
fun GridItem(number: Int) { | |
Box( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(8.dp) | |
.height(100.dp) | |
.background(Color.LightGray), | |
contentAlignment = Alignment.Center | |
) { | |
Text( | |
text = number.toString(), | |
fontSize = 24.sp, | |
fontWeight = FontWeight.Bold, | |
color = Color.Black | |
) | |
} | |
} | |
@Preview | |
@Composable | |
fun PreviewLazyVerticalGridExample() { | |
LazyVerticalGridExample() | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun GreetingPreview() { | |
DefaultTheme { | |
LazyVerticalGridExample() | |
} | |
} |

'Compose' 카테고리의 다른 글
Box (0) | 2023.05.13 |
---|---|
LazyVerticalGrid + Navigation (0) | 2023.05.11 |
compose 계산기 수정 (0) | 2023.05.10 |
compose 계산기 (0) | 2023.05.10 |
navigation (0) | 2023.05.07 |