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

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

by immgga 2022. 12. 3.

์ง€๋‚œ ํฌ์ŠคํŒ…

https://rkdrkd-history.tistory.com/41

 

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

1. layout 1. Column ํ•˜์œ„์˜ ๋ทฐ๋“ค์„ ์ˆ˜์ง ์ •๋ ฌํ•ด์ค€๋‹ค(vertical). // ํ…์ŠคํŠธ ๋ทฐ @Composable fun Greeting(name: String) { Text(text = name) } // ๋ทฐ๋ฅผ ๊ทธ๋ฆฌ๋Š” ๊ณณ(๋ฏธ๋ฆฌ๋ณด๊ธฐ) @Preview(showBackground = true) @Composable fun DefaultPreview() {

rkdrkd-history.tistory.com

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ•„์ž๊ฐ€ xml์„ ์‚ฌ์šฉํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ทฐ๋“ค์„ compose๋กœ ์ž‘์„ฑํ•ด๋ณด๊ฒ ๋‹ค.


2. ๋‹ค์–‘ํ•œ ๋ทฐ ์ž‘์„ฑ

1. edit text

TextField๋ฅผ ์‚ฌ์šฉํ•ด ํ…์ŠคํŠธ ์ž…๋ ฅ ์นธ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

@Composable
fun Greeting2(name: String) {
    Text(text = name)
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
    ComposeUITheme {
        Box(modifier = Modifier
            .fillMaxWidth()
            .height(120.dp)) {

            Column(modifier = Modifier.padding(15.dp)) {
                Greeting2("Hello")
                val text = remember { mutableStateOf("") }
                TextField(value = text.value, onValueChange = { textValue -> text.value = textValue })
            }
        }
    }
}

๋ณด๊ธฐ ํŽธํ•˜๊ฒŒ Box ์•ˆ์—์„œ ์ž‘์—…์„ ํ–ˆ๊ณ , padding๋„ 15dp๋งŒํผ ์ฃผ์—ˆ๋‹ค.

์„ค์ •ํ•œ ํ…์ŠคํŠธ์™€ TextField๊ฐ€ ์ž˜ ๋‚˜ํƒ€๋‚œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

์‹คํ–‰ ํ™”๋ฉด

 

2. imageView

์ด๋ฏธ์ง€ ๋ทฐ๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์€ Image๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

@Composable
fun ImageView() {
    Image(painter = painterResource(id = R.drawable.ic_launcher_background),
        contentDescription = null   // ํ•„์ˆ˜ ์š”์†Œ
    )
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
    ComposeUITheme {
        Box(modifier = Modifier
            .fillMaxWidth()
            .height(120.dp)
            .padding(15.dp)) {

            ImageView()
        }
    }
}

image ์•ˆ์— contentDescription์€ ์“ฐ์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ์จ๋†“๋„๋ก ํ•˜์ž.

 

์‹คํ–‰ ํ™”๋ฉด

 

์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ ์กฐ์ ˆ ๋ฐฉ๋ฒ•

@Composable
fun ImageView() {
    Image(painter = painterResource(id = R.drawable.ic_launcher_background),
        contentDescription = null,
        modifier = Modifier.width(150.dp).height(150.dp)
    )
}

modifier ์•ˆ์— width์™€ height๋ฅผ ๊ฐ๊ฐ ์ž…๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

์˜ˆ์ œ 1

๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ

@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
    ComposeUITheme {
        Box(modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()) {

            Column(modifier = Modifier.align(Alignment.Center)) {
                Image(painter = painterResource(id = R.drawable.practice_example_1),
                    contentDescription = null,
                    modifier = Modifier
                        .width(150.dp)
                        .height(150.dp)
                        .align(Alignment.CenterHorizontally)
                        .offset(y = (-20).dp))

                val text = remember { mutableStateOf("") }
                val password = remember { mutableStateOf("") }
                // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณด์ด๊ฒŒ ํ•˜๋Š” ์—ฌ๋ถ€
                var passwordVisibility by remember { mutableStateOf(false) }
                val icon = if (passwordVisibility) {
                    painterResource(id = R.drawable.ic_baseline_visibility_24)
                } else painterResource(id = R.drawable.ic_baseline_visibility_off_24)
                // ์ด๋ฉ”์ผ
                TextField(value = text.value, onValueChange = { textValue -> text.value = textValue },
                    label = {Text(text = "email")}, modifier = Modifier.offset(y = (-10).dp))
                // ๋น„๋ฐ€๋ฒˆํ˜ธ
                TextField(value = password.value, onValueChange = { textValue -> password.value = textValue },
                    label = {Text(text = "password")},
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
                    trailingIcon = {
                        IconButton(onClick = {
                            passwordVisibility = !passwordVisibility
                        }) {
                            Icon(painter = icon, contentDescription = null)
                        }
                    },
                    visualTransformation = if (passwordVisibility) VisualTransformation.None
                    else PasswordVisualTransformation()
                )
            }
        }
    }
}

TextField ์†์„ฑ

  • keyboardOptions: ํ‚ค๋ณด๋“œ์˜ ์†์„ฑ์„ ์ •์˜ํ•œ๋‹ค.(๋น„๋ฐ€๋ฒˆํ˜ธ, ์ˆซ์ž, ์ด๋ฉ”์ผ ๋“ฑ๋“ฑ)
  • trailingIcon: ํ…์ŠคํŠธ๋ฅผ visibility๋ฅผ ์„ค์ •ํ•˜๋Š” icon์„ ์„ค์ •ํ•˜๊ฒŒ ํ•ด ์ค€๋‹ค.
  • visualTransformation: ํ…์ŠคํŠธ์˜ visibility์„ ์„ค์ •ํ•œ๋‹ค.

text์™€ password์˜ mutableState๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•œ ์ด์œ ๋Š”ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๋ฉด 1๊ฐœ์˜ TextField์˜ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ๋‹ค๋ฅธ TextField์˜ ๊ฐ’์ด ์ž๋™์œผ๋กœ ์„ค์ • ์ค‘์ธ ๊ฐ’์œผ๋กœ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

ํ…์ŠคํŠธ visibility๊ฐ€ ์ž˜ ์„ค์ •๋˜์—ˆ๋‹ค.

728x90