[TIL]220928 - Kotlin layout

Jimin·2022년 10월 2일
0
post-thumbnail
post-custom-banner

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApp()
        }
    }
}

@Composable
fun MyApp() {
    KOTLIN_layoutTheme {
        // A surface container using the 'background' color from the theme
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.background
        ) {
            MyLinearLayout()    
        //Greeting("Android")
        }
    }
}

@Composable
fun MyLinearLayout() {
    Column() {
        Row() {
            Text(text = "안녕하세요",
                modifier = Modifier
                    .background(Color.Yellow)
                    .padding(8.dp)
            )
            Text(text = "컴퓨터공학과",
                modifier = Modifier
                    .background(Color.Cyan)
                    .padding(8.dp)

            )
        }
        Row() {
            Text(text = "어디서 본 것 같은 예제",
                modifier = Modifier
                    .background(Color.Magenta)
                    .padding(8.dp)

            )
            Text(text = "2020607",
                modifier = Modifier
                    .background(Color.Gray)
                    .padding(8.dp)

            )
        }

        Row() {
            Text(text = "어디서 본 것 같은 예제",
                modifier = Modifier
                    .background(Color.Magenta)
                    .padding(8.dp)

            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApp()
}


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApp {
                Counter()
            }
        }
    }
}

@Composable
fun MyApp(content: @Composable () -> Unit) {
    Kotiln_counterTheme {
        // A surface container using the 'background' color from the theme
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.background
        ) {
            content()
        }
    }
}

@Composable
fun Counter() {
    var text by remember { mutableStateOf(0) }

    Column(
        modifier = Modifier
            //.fillMaxSize()
            .padding(8.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = text.toString(),
            fontSize = 70.sp,
        )
        Row() {
            Button( modifier = Modifier.weight(1f),
                onClick = {
                    text++
                }) {
                Text(text = "증가")
            }
            Spacer(modifier = Modifier.weight(0.5f))
            Button( modifier = Modifier.weight(1f),
                onClick = {
                    if (text > 0) text--
                }) {
                Text(text = "감소")
            }
        }


    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    Kotiln_counterTheme {
        Counter()
    }
}
post-custom-banner

0개의 댓글