๋ชฉ์ฐจ
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
'2023 ์๋๋ก์ด๋ > ์๋๋ก์ด๋ ์คํฐ๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Unit1- Jetpack Compose๋ ๋ฌด์์ธ๊ฐ์? (0) | 2022.12.24 |
---|---|
4.Compose ์ฌ๋ถ๋ฉด (0) | 2022.11.17 |
10. ์๋ฃจ์ ์ฝ๋ ๊ฐ์ ธ์ค๊ธฐ ~ 11. ๊ฒฐ๋ก (0) | 2022.11.16 |
Android ์ฑ์ ์ด๋ฏธ์ง ์ถ๊ฐ [ ๋์ ๊ณผ์ ] (0) | 2022.11.16 |
Android ์ฑ์ ์ด๋ฏธ์ง ์ถ๊ฐ 6~ (0) | 2022.11.16 |