๐Ÿ”ฅTIL๐Ÿ”ฅJetpack Compose | Component

hyihyiยท2024๋…„ 4์›” 2์ผ
1

Compose

๋ชฉ๋ก ๋ณด๊ธฐ
3/4
post-thumbnail
post-custom-banner

๐Ÿ“– Compose๋ž€?

Xml์ด ์•„๋‹Œ Kotlin ๋ฌธ๋ฒ•์œผ๋กœ view๋ฅผ ์ •์˜.
View์™€ ๋™์ž‘์˜ ์ƒํ˜ธ๊ด€๊ณ„, ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” API๋ฅผ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ œ๊ณตํ•ด์คŒ


๐Ÿค” Compose๋ฅผ ์™œ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€?

1. ์ ์€ ์ฝ”๋“œ๋Ÿ‰

๊ณ„์ธต ๊ตฌ์กฐ๋กœ ํ‘œํ˜„์ด XML์— ๋น„ํ•ด ๊ฐ„๊ฒฐํ•˜๋‹ค.
๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋œ ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ทฐ๋ฅผ ์ง์ ‘ ์ฐพ์•„์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

2. ์ง๊ด€์ ์ธ ์ฝ”๋“œ

์„ ์–ธํ˜• UI ๋ฐฉ์‹์œผ๋กœ ์ˆœ์ˆ˜ํ•˜๊ฒŒ UI ํ‘œํ˜„์—๋งŒ ์ง‘์ค‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ง๊ด€์ .
์ปดํฌ์ฆˆ ํ‘œํ˜„์— ์ž๋™ ๋ฐ”์ธ๋”ฉ ๋˜์–ด ๋” ์ด์ƒ ๋ทฐ์˜ ์ƒํƒœ๋ฅผ ์ˆ˜์ž‘์—…์œผ๋กœ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

3. ์ƒ์‚ฐ์„ฑ ํ–ฅ์ƒ

Navigation, ViewModel, Coroutine ๋“ฑ ํ”„๋ ˆ์ž„์›Œํฌ ๋ ˆ๋ฒจ์—์„œ ์ ๊ทน์ ์œผ๋กœ ์ง€์›ํ•œ๋‹ค.
์ฝ”๋“œ์—์„œ ๋™์ ์œผ๋กœ ๋ทฐ ๋กœ์ง ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

4. ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ ์ง€์›

MaterialDesign, Theme ๋“ฑ ๋””์ž์ธ ์ธก๋ฉด์—์„œ ๊ฐ„ํŽธํ•œ API ๊ตฌ์กฐ์™€ ๋‹ค์–‘ํ•œ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
Animation API๊ฐ€ ๊ฐ•๋ ฅํ•ด์กŒ๋‹ค.


๐Ÿ“ Declarative UI (์„ ์–ธํ˜• UI)

  • ์˜ค๋กœ์ง€ ์–ด๋–ค UI๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”์ง€์—๋งŒ ์ง‘์ค‘ํ•˜๋Š” UI ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•๋ก 

์„ ์–ธํ˜• UI๋Š” ์˜ค๋กœ์ง€ ๋ทฐ๊ฐ€ ์–ด๋–ค ์ƒํƒœ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š”์ง€, ๋ทฐ์—์„œ ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€์—๋งŒ ๊ด€์‹ฌ์ด ์žˆ์Œ
๊ฐœ๋ฐœ์ž๋Š” View์— State๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๊ณ  ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š”๋ฐ ์ง‘์ค‘

@Composable
fun Gretting() {
	val name by remember { mutableStateOf("Ethan") }
    val count by remember { mutableStateOf(1) }
    
    Column {
    	Text(text = "Name: $name")
        Text(text = "Count: $count")
    }
|

name, count๊ฐ€ ์–ธ์ œ ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ๋Š”์ง€ UI๋Š” ์ „ํ˜€ ๊ด€์‹ฌ ์—†๋‹ค.

๋ช…๋ นํ˜• UI : name ์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ฐœ๋ฐœ์ž๊ฐ€ setText๋กœ ๋ทฐ์— text๋ฅผ ์ง์ ‘ ๋ฐ”๊พผ๋‹ค.
์„ ์–ธํ˜• UI : ํ…์ŠคํŠธ์— name ์ƒํƒœ๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋˜์–ด ์žˆ๋‹ค. name์ด ๋ฐ”๋€Œ๋ฉด UI๊ฐ€ ์•Œ์•„์„œ ๋ฐ”๊พผ๋‹ค.

๐Ÿ’ก์œ ์ง€๋ณด์ˆ˜ ์ธก๋ฉด์—์„œ ๊ฐœ๋ฐœ์ž๊ฐ€ ์‹ ๊ฒฝ์จ์•ผ ํ•  ๋ฐ์ดํ„ฐ์™€ ์ƒํƒœ ์—ฐ๊ฒฐ์„ ์†์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•ด์คŒ

์›๋ฆฌ : ๋ทฐ๋ฅผ ๋งค๋ฒˆ ์ƒˆ๋กœ ๊ทธ๋ฆผ


๐Ÿ“ Compose UI ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

โœ‹ @Composable ํ•จ์ˆ˜

  • ์ปดํฌ์ฆˆ ๋ทฐ๋ฅผ ์˜๋ฏธ.
  • Composable์€ ํ•ฉ์„ฑ์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ๊ตฌ์„ฑ ์š”์†Œ
    ex) ๋ ˆ๊ณ  ์กฐ๊ฐ
  • Composable์€ ๋‹จ๋…์œผ๋กœ๋„ ๋ทฐ๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ณ  ์—ฌ๋Ÿฌ Composable๋กœ ํ•ฉ์„ฑ๋œ ๋ทฐ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ์Œ
    ex) ๋ ˆ๊ณ  ์กฐ๊ฐ์„ ์กฐ๋ฆฝํ•˜๋Š” ๊ฒƒ์„ ์—ฐ์ƒ

โœ‹ ํ•จ์ˆ˜ ๊ทœ์น™

  • @Composable์„ ์„ค์ •ํ•ด์•ผ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ปดํฌ์ฆˆ ๋ทฐ๋กœ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹ค๋ฅธ ์ปดํฌ์ €๋ธ” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„
  • ๋ฆฌํ„ด์„ ํ•˜์ง€ ์•Š์Œ
  • Idempotent ํ•ด์•ผ ํ•จ

๐Ÿ“ Compose ๋‚ด์žฅ ์ปดํฌ๋„ŒํŠธ

โœ‹ 1. Text

xml์˜ TextView

  • text ์†์„ฑ ํ•˜๋‚˜๋งŒ ํ•„์ˆ˜ ์ธ์ž

xml๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ”„๋ ˆ์ž„์›Œํฌ ์ฐจ์›์—์„œ ๋ฌธ์ž, ๋ฒ„ํŠผ, ๋ ˆ์ด์•„์›ƒ ๋“ฑ ๊ธฐ๋ณธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ œ๊ณต

โœ‹ 2. Button

xml์˜ Button

  • ํด๋ฆญํ•  ๋•Œ ์ˆ˜ํ–‰ํ•  onClick ๋žŒ๋‹ค์™€ ๋ฒ„ํŠผ ๋‚ด๋ถ€์˜ ๋ทฐ๋ฅผ ์„ค์ •ํ•  content๋ฅผ ํ•„์ˆ˜๋กœ ์„ค์ •ํ•ด์•ผ ํ•จ
  • ๋‚ด๋ถ€ ๋ทฐ๋ฅผ ํฌํ•จํ•œ ๊ฐœ๋…์ด ์•„๋‹ˆ๋ผ ๋ณ„๋„๋กœ ๋‚ด๋ถ€ ๋ทฐ๋ฅผ ์ž‘์„ฑํ•ด์„œ ๋„ฃ์–ด์ค˜์•ผ ํ•จ
  • xml์—์„œ๋Š” ๋ทฐ๊ฐ€ ์ƒ์†์„ ํ†ตํ•ด ๋งŒ๋“ค์–ด์กŒ๋‹ค๋ฉด ์ปดํฌ์ฆˆ๋Š” ํ™•์žฅ์„ ํ†ตํ•ด ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ์›์น™์œผ๋กœ ํ•จ

โœ‹ 3. TextField

xml์˜ EditText

  • value์™€ onValueChange๊ฐ€ ํ•„์ˆ˜ ์ธ์ž
  • ์„ ์–ธํ˜• UI์˜ ํŠน์„ฑ์ƒ UI๋Š” ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ๊ณผ, ์ด๋ฒคํŠธ ๋ฐฉ์ถœ ๋‘ ๊ฐ€์ง€๋งŒ ์ง‘์ค‘
@Composable
fun Gretting() {
	var name by remember { mutableStateOf("") }
    TextField(
    	value = name,
        onValueChange = { name = it }
    )
}
  1. ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ : TextField์˜ value = name
  2. ์ด๋ฒคํŠธ ๋ฐฉ์ถœ : value๊ฐ€ ๋ฐ”๋€Œ๋ฉด onValueChange ์ด๋ฒคํŠธ ๋ฐœ์ƒ

โœ‹ 4. Layout - Column

xml์˜ LinearLayout(orientation = vertical)

  • verticalArrangement : ์ˆ˜์ง ๋ฐฉํ–ฅ ์ •๋ ฌ
  • horizontalAlignment : ์ˆ˜ํ‰ ๋ฐฉํ–ฅ ์ •๋ ฌ
@Composable
inline fun Column(
	modifier: Modifier = Modifier,
    verticalArrangement: Arrangement.Vertical = Arrangement.Top,
    horizontalAlignment: Alignment.Horizontal = Alignment.Start,
    content: @Composable ColumnScope.() -> Unit
) {

5. Layout - Row

xml์˜ LinearLayout(orientation = horizontal)

  • horizontalArrangement : ์ˆ˜ํ‰ ๋ฐฉํ–ฅ ์ •๋ ฌ
  • verticalAlignment : ์ˆ˜์ง ๋ฐฉํ–ฅ ์ •๋ ฌ
@Composable
inline fun Row(
	modifier: Modifier = Modifier,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    verticalAlignment: Alignment.Vertical = Alignment.Top,
    content: @Composable RowScope.() -> Unit
) {

6. Layout - Box

xml์˜ FrameLayout

  • contentAlignment ; ์ปจํ…์ธ  ์ •๋ ฌ
    propagateMinConstraints : ์ตœ์†Œ ์‚ฌ์ด์ฆˆ ์ œ์•ฝ ์กฐ๊ฑด ์ „ํŒŒ ์—ฌ๋ถ€(์ž์‹์ด ๋ถ€๋ชจ์˜ ์ตœ์†Œ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์„ค์ •๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ)
@Composable
inline fun Box(
	modifier: Modifier = Modifier,
    contentAlignment: Alignment = Alignment.TopStart,
    propagateMinConstraints: Boolean = false,
    content: @Composable BoxScope.() -> Unit
) {

โœ‹ List

7. List - LazyColumn

์ˆ˜์ง ๋ฐฉํ–ฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์ปดํฌ๋„ŒํŠธ
๋ชจ๋“  ๋ฆฌ์ŠคํŠธ์˜ ๋ทฐ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ํ•„์š”ํ•œ๋งŒํผ ๋ทฐ๋ฅผ ์ƒ์„ฑ

LazyColumn {
    val numbers = (0..100).toList()
    items(numbers){
        Text(
            text = "Number: $it",
            modifier = Modifier.height(200.dp)
        )
    }
}

XML View์— ์—ฐ๊ด€๋œ Compose Component


๐Ÿ“ Modifier

์ปดํฌ๋„ŒํŠธ๋“ค์„ ๊พธ๋ฏธ๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ˆ˜์ •์ž

@Composable
fun ModifierTest1(name: String){
    Column(
        modifier = Modifier
            .size(300.dp)
            .padding(24.dp)
            .fillMaxSize()
            .clickable { Log.d("TAG", "clicked!") }
    ){
        Text(text = "Hello")
        Text(text = ", $name")
    }
}

โœ‹Modifier๋กœ ์ž์ฃผ ์“ฐ๋Š” ๊ธฐ๋Šฅ

1. size

2. padding

3. fillMaxSize

4. clickable

Modifier๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋จผ์ € ํ˜ธ์ถœ๋œ ์„ค์ •์ด ์šฐ์„ ์ด๋ฏ€๋กœ padding์„ ์„ค์ •ํ•˜๊ณ  Modifier์„ clickable๋ณด๋‹ค ๋จผ์ € ์ž‘์„ฑํ•˜๋ฉด padding ์„ค์ • ์ „ ์‚ฌ์ด์ฆˆ๋งŒํผ ํด๋ฆญ ์˜์—ญ์ด ์„ค์ •๋œ๋‹ค.

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€