๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ| Android/๐Ÿš€ | Jetpack

[Android, Kotlin] Compose UI ์ •๋ฆฌ(1)

by immgga 2022. 12. 1.

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” jetpack compose

1. layout

1. Column

ํ•˜์œ„์˜ ๋ทฐ๋“ค์„ ์ˆ˜์ง ์ •๋ ฌํ•ด์ค€๋‹ค(vertical).

// ํ…์ŠคํŠธ ๋ทฐ
@Composable
fun Greeting(name: String) {
    Text(text = name)
}

// ๋ทฐ๋ฅผ ๊ทธ๋ฆฌ๋Š” ๊ณณ(๋ฏธ๋ฆฌ๋ณด๊ธฐ)
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeUITheme {    // ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ํ…Œ๋งˆ
        Column() {
            Greeting("Hello")
            Greeting("Android")
        }
    }
}

2. Row

ํ•˜์œ„์˜ ๋ทฐ๋“ค์„ ์ˆ˜ํ‰ ์ •๋ ฌํ•ด์ค€๋‹ค(horizontal).

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeUITheme {    // ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ํ…Œ๋งˆ
        Row() {
            Text(text = "Hello Android!")
            Text(text = "Content!")
        }
    }
}

3. box

์—ฌ๋Ÿฌ ๋ทฐ๋“ค์„ ๊ฒน์ณ์„œ ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๋ ˆ์ด์•„์›ƒ์ด๋‹ค.(framelayout๊ณผ ๋น„์Šทํ•˜๋‹ค.)

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeUITheme {    // ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ํ…Œ๋งˆ
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(100.dp),
        ) {
            Text(text = "Hello Android!")
            Text(text = "Content!")
        }
    }
}

modifier๋ฅผ ์‚ฌ์šฉํ•ด box์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  •  fillMaxWidth(): width๋ฅผ match_parent๋กœ ์„ค์ •ํ•œ๋‹ค.
  • fillMaxHeight(): height๋ฅผ match_parent๋กœ ์„ค์ •ํ•œ๋‹ค.

์œ„์— ๋‘ ๊ฐœ ๋ง๊ณ ๋„ (์›ํ•˜๋Š” ํฌ๊ธฐ.dp)๋ฅผ ์‚ฌ์šฉํ•ด ํฌ๊ธฐ๋ฅผ ์ž์œ ์ž์žฌ๋กœ ์กฐ์ ˆ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์•„๋ฌด ์Šคํƒ€์ผ๋„ ์ ์šฉํ•˜์ง€ ์•Š์€ box

 

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeUITheme {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(100.dp),
            contentAlignment = Alignment.TopCenter
        ) {
            Row() {
                Text(text = "Hello Android!")
                Text(text = "Content!")
            }
        }
    }
}
  • contentAlignment: ๋ทฐ๋“ค์˜ ์œ„์น˜๋ฅผ ์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

contentAlignment์™€ row๋ฅผ ์ ์šฉํ•œ box

 

contentAlignment๋ฅผ box์— ์“ฐ๋ฉด ํ•˜์œ„ ๋ทฐ์˜ ์œ„์น˜๋ฅผ ๋ชจ๋‘ ์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฐœ๋ณ„๋กœ ์œ„์น˜๋ฅผ ์ ์šฉํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ์—๋Š”

text์— modifier๋ฅผ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•˜๋‹ค.

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeUITheme {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(100.dp),
        ) {
            Text(text = "Hello Android!", modifier = Modifier.align(Alignment.TopStart))
            Text(text = "Content!", modifier = Modifier.align(Alignment.BottomEnd))
        }
    }
}

ํ…์ŠคํŠธ 2๊ฐœ๋ฅผ ๊ฐ๊ฐ ์™ผ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์•„๋ž˜์— ๋ฐฐ์น˜ํ•œ ๋ชจ์Šต์ด๋‹ค.

  • Start: ์˜ค๋ฅธ์ชฝ
  • Top: ์œ„์ชฝ
  • Center: ๊ฐ€์šด๋ฐ
  • End: ์™ผ์ชฝ
  • Bottom: ์•„๋ž˜์ชฝ
728x90