๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2023 ์•ˆ๋“œ๋กœ์ด๋“œ/์•ˆ๋“œ๋กœ์ด๋“œ ์Šคํ„ฐ๋””

์—ฐ์Šต: Compose ๊ธฐ๋ณธ ์‚ฌํ•ญ

by hyeonha 2022. 11. 17.

๋ชฉ์ฐจ

    1 ) 

    MainActivity.kt

    package com.example.learntogether
    
    import android.os.Bundle
    import android.text.style.TextAppearanceSpan
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.Image
    import androidx.compose.foundation.layout.*
    import androidx.compose.material.MaterialTheme
    import androidx.compose.material.Surface
    import androidx.compose.material.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.res.painterResource
    import androidx.compose.ui.res.stringResource
    import androidx.compose.ui.text.style.TextAlign
    import androidx.compose.ui.tooling.preview.Preview
    import androidx.compose.ui.unit.dp
    import androidx.compose.ui.unit.sp
    import com.example.learntogether.ui.theme.LearnTogetherTheme
    import org.w3c.dom.Text
    import kotlin.text.Typography.less
    
    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                LearnTogetherTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(
                        modifier = Modifier.fillMaxSize(),
                        color = MaterialTheme.colors.background
                    ){
    
                        LearnTogether(
                            txt1 = stringResource(R.string.txt1)
                        ,txt2 = stringResource(R.string.txt2)
                            ,txt3 = stringResource(R.string.txt3)
                        )
    
    
                    }
    
                }
            }
        }
    }
    @Composable
    fun LearnTogether(txt1: String, txt2: String, txt3:String) {
        val image = painterResource(R.drawable.bg_compose_background)
        Column {
        Image(
            painter = image,
            contentDescription = null,
            modifier = Modifier.fillMaxWidth()
        ) //Image
    
    
        Text(
            text = txt1 ,
            fontSize = 24.sp,
            modifier = Modifier
                .padding(start = 16.dp, top = 16.dp, end = 16.dp, bottom = 16.dp)
        )
        Text(
            text = txt2,
            modifier = Modifier
                .padding(start = 16.dp, end = 16.dp),
            textAlign = TextAlign.Justify
        )
            Text(
                text = txt3,
                modifier = Modifier
                    .padding(start = 16.dp, end = 16.dp, top = 16.dp, bottom = 16.dp),
                textAlign = TextAlign.Justify
    
    
            )
    }
    } //LearnTogether
    
    
    @Preview(showBackground = true)
    @Composable
    fun DefaultPreview() {
        LearnTogetherTheme {
            LearnTogether(
                txt1 = stringResource(R.string.txt1),
                txt2 = stringResource(R.string.txt2),
                txt3 = stringResource(R.string.txt3)
            )
        }
    }

    strings.xml

    <resources>
        <string name="app_name">Learn Together</string>
        <string name="txt1">Jetpack Compose tutorial</string>
        <string name="txt2">Jetpack Compose is a modern toolkit for building native Android UI. Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs.</string>
        <string name="txt3">In this tutorial, you build a simple UI component with declarative functions. You call Compose functions to say what elements you want and the Compose compiler does the rest. Compose is built around Composable functions. These functions let you define your app\'s UI programmatically because they let you describe how it should look and provide data dependencies, rather than focus on the process of the UI\'s construction, such as initializing an element and then attaching it to a parent. To create a Composable function, you add the @Composable annotation to the function name.</string>
    </resources>

    ๊ฒฐ๊ณผํ™”๋ฉด

     

    2) 

    q. column() ๋กœ ํ•˜๋ฉด ์•ˆ๋˜๊ณ  column{ } ๋กœ ํ•˜๋ฉด ๋˜๋Š” ์ด์œ 

    q. box ๋ ˆ์ด์•„์›ƒ์€ ์–ธ์ œ ์“ฐ๋‚˜? column์€ ์—ด๋ฐฉํ˜•์œผ๋กœ ์ •๋ ฌ, row๋Š” ํ–‰๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ , ๊ทธ๋Ÿฌ๋ฉด box๋Š”?

     

     

    ๊ฒฐ๊ณผํ™”๋ฉด

     

    ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ํ–ˆ๋Š”๋ฐ ๊ฒฐ๊ณผํ™”๋ฉด ์ฒ˜๋Ÿผ์˜ ์—ฌ์œ ๊ณต๊ฐ„์„ ์–ด๋–ป๊ฒŒ ์ค˜์•ผํ•˜์ง€


    horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center 


      modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(),

    -> ํ•ด์ฃผ๋ฉด ์—ฌ์œ ๊ณต๊ฐ„์ด ์ƒ๊ธด๋‹ค.

     

    @Composable
    fun TaskCompleted() {
        Column( modifier = Modifier
            .fillMaxHeight()
            .fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center){
            val image = painterResource(R.drawable.ic_task_completed)
            Image(painter = image, contentDescription =null )
            Text(
                text = stringResource(R.string.txt1),
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(top=24.dp, bottom = 8.dp)
            )
            Text(
                text= stringResource(R.string.txt2),
                fontSize = 16.sp
            )
             }
        }
    

    3) Compose ์‚ฌ๋ถ„๋ฉด 

     

    728x90