#1 ๊ฐ์
#1-1 ์ฒซ ์ฝ ๋จ๊ธฐ
๋ฌด์๋ถํฐ ์์ ๋์ผํ ์ง ๊ฐ์ด ์กํ์ง ์๋๋ค. ํ๋กํ ํ์ดํ์ ํ๋ฒ ๋ ํด์ผ ํ๋? ์ํํธ์จ์ด ๋ชจ๋ธ๋ง์ด๋ผ๋ ๊ฑด ๋ญ์ง... ์ด๊ฑธ ๋จผ์ ๊ณต๋ถํด์ผํ ๊น? ... ์ฌ๋ฌ ์๋ฌธ์ด ๋ ๋ค. ๊ทธ๋๋ ๋ถ๋ช ํ ๊ฑด ์์ผ๋ก ๋ด๊ฐ ํต์ฌ์ ์ผ๋ก ๊ณต๋ค์ฌ์ผํ ๋ฌด์ธ๊ฐ(ํต์ฌ ๊ณผ์ )๋ ๋ถ๋ช ์กด์ฌํ ๊ฒ์ด๋ผ๋ ์ฌ์ค์ด๋ค. ๊ฐ๋ น ํ๋ก ํธ์๋์์ ๋งค๋๋ฌ์ด ์ ๋๋ฉ์ด์ ์ ๊ตฌํํด์ผํ๋ค๊ฑฐ๋ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๋ง๋ํ๊ฒ ์ฒ๋ฆฌํ๋ ์์ ๋ค์ด ๊ทธ ์์ผ ๊ฒ์ด๋ค. ๊ทธ ๊ณผ์ ๊น์ง ๋ฟ๊ธฐ ์ํด์ ๋จผ์ ํ์คํ ํด๋์ด์ผ ํ๋ ๊ฒ๋ค๋ถํฐ ๋จผ์ ์ฒ๋ฆฌํด์ผํ๋ค.
#1-2 Scaffold
[Android] Jetpack Compose - Scaffold
#1 ๊ฐ์#1-1 Scaffold์ ์ฌ์ ์ ์๋ฏธScaffold๋ ๋น๊ณ((๊ฑด์ค) ๋์ ๊ณณ์์ ๊ณต์ฌ๋ฅผ ํ ์ ์๋๋ก ์์๋ก ์ค์นํ ๊ฐ์ค๋ฌผ)๋ผ๋ ๋จ์ด๋ก ๋ฒ์ญ๋๋ค. #1-2 Scaffold in Jetpack Compose Jetpack Compose | Android Developers์ด
kenel.tistory.com
์ฐ์ ๋๋ ํ๋ก ํธ์๋๋ก Jetpack Compose๋ฅผ ์ฌ์ฉํ ๊ฒ์ด๊ณ Scaffold๋ ๋น์ฐํ ๋ค์ด๊ฐ๋ค. ๋ฐ๋ผ์ Jetpack Compose์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ผ๋๋ผ ๋ณผ ์ ์๋ Scaffold๋ถํฐ ์ง์ด๋ฃ๊ณ ๋ณด๋ ค๊ณ ํ๋ค. ์ ๊ฒ์๊ธ์ ๊ธฐ๋ฐํด์ ์๋ ์ฝ๋์ ๊ฐ์ด Scaffold๋ฅผ ๊ตฌ์ฑํ๋ค.
#2 ์ฝ๋
#2-1 MainScreen์ Scaffold
...
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
NutricapturenewTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = { },
bottomBar = { },
snackbarHost = { }
) { innerPadding ->
SampleContent(
text = "Sample Content",
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.background(Color.LightGray)
)
}
}
}
}
}
@Composable
fun SampleContent(
text: String,
modifier: Modifier = Modifier
) {
Box(
modifier = modifier
) {
Button(
onClick = { },
modifier = Modifier.align(Alignment.Center)
) {
Text(text = text)
}
}
}
Scaffold์์ ๋ด๊ฐ ์ฌ์ฉํ ์์๋ topBar, bottomBar, snackbar๋ก ์์๋๋ค.
#2-2 Scaffold์ topBar
Scaffold(
...
topBar = {
TopAppBar(
title = { Text("Sample Top Bar Text") }
)
},
...
) { innerPadding ->
...
}
๊ฐ๋จํ๊ฒ๋ง ๊ตฌํํด๋์๋ค. ๋์ค์๋ ๋ฒํผ ๋ฑ์ ๋ค๋ฅธ ์์๋ค๋ ์ถ๊ฐํ ๊ฒ์ด๋ค.
#2-3 Scaffold์ bottomBar
Scaffold(
...
bottomBar = {
BottomAppBar(
content = { Text("Sample Bottom Bar Text") }
)
},
...
) { innerPadding ->
...
}
BottomAppBar๋ ์ผ๋จ ์ฑ์๋ฃ๊ธฐ์ฉ์ผ๋ก ์ฌ์ฉํ๋ค. ์ค์ ๋ก๋ BottomAppBar ๋์ NavigationBar๋ฅผ ์ฌ์ฉํ ๊ฒ์ด๋ค. ์ฌ๊ธฐ์์ ๊ตฌํํ๋ค.
#2-4 Scaffold์ snackbarHost
...
class MainActivity : ComponentActivity() {
...
override fun onCreate(savedInstanceState: Bundle?) {
...
setContent {
NutricapturenewTheme {
// Snackbar๋ฅผ ์ํ CoroutineScope์ State
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
Scaffold(
...
snackbarHost = {
SnackbarHost(
hostState = snackbarHostState,
snackbar = { snackbarData ->
Snackbar(
modifier = Modifier.padding(12.dp),
) {
Text(
text = snackbarData.visuals.message,
)
}
}
)
}
) { innerPadding ->
SampleContent(
...
scope = scope,
snackbarHostState = snackbarHostState
)
}
}
}
}
}
@Composable
fun SampleContent(
...
scope: CoroutineScope = rememberCoroutineScope(),
snackbarHostState: SnackbarHostState = remember { SnackbarHostState() }
) {
Box(
...
) {
Button(
onClick = {
scope.launch {
snackbarHostState.showSnackbar(
message = "Button Clicked",
duration = SnackbarDuration.Short
)
}
},
...
) {
...
}
}
}
์ฌ์ฉ์์๊ฒ ์ ๋ฌํ ๊ฐ์ข ๋ฉ์์ง๋ฅผ Snackbar๋ฅผ ํตํด ๋ณด๋ผ ๊ฐ๋ฅ์ฑ์ด ์์ฃผ ๋์ผ๋ฏ๋ก, Snackbar๋ ๋ง๋ค์ด๋๋ค. ๊ด๋ จ ๋ณ์์ธ scope์ snackbarHostState๋ฅผ ์ธ์๋ก ๋ฐ๊ฒ๋ SampleContent()์ ์์ ํ๊ณ , onClick ๊ตฌํ์ ์ฌ์ฉํ๋ค.
#2-5 ์ ์ฒด ์ฝ๋
package com.example.nutri_capture_new
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.nutri_capture_new.ui.theme.NutricapturenewTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
NutricapturenewTheme {
// Snackbar๋ฅผ ์ํ CoroutineScope์ State
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
TopAppBar(
title = { Text("Sample Top Bar Text") }
)
},
bottomBar = {
BottomAppBar(
content = { Text("Sample Bottom Bar Text") }
)
},
snackbarHost = {
SnackbarHost(
hostState = snackbarHostState,
snackbar = { snackbarData ->
Snackbar(
modifier = Modifier.padding(12.dp),
) {
Text(
text = snackbarData.visuals.message,
)
}
}
)
}
) { innerPadding ->
SampleContent(
text = "Sample Content",
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.background(Color.LightGray),
scope = scope,
snackbarHostState = snackbarHostState
)
}
}
}
}
}
@Composable
fun SampleContent(
text: String,
modifier: Modifier = Modifier,
scope: CoroutineScope = rememberCoroutineScope(),
snackbarHostState: SnackbarHostState = remember { SnackbarHostState() }
) {
Box(
modifier = modifier
) {
Button(
onClick = {
scope.launch {
snackbarHostState.showSnackbar(
message = "Button Clicked",
duration = SnackbarDuration.Short
)
}
},
modifier = Modifier.align(Alignment.Center)
) {
Text(text = text)
}
}
}
#3 ์์ฝ
๋น ํ๋ก์ ํธ์ ์ผ๋จ Scaffold๋ถํฐ ๋ง๋ค์๋ค.
#4 ์์ฑ๋ ์ฑ
#4-1 ์คํฌ๋ฆฐ์ท

#4-2 ์ด ๊ฒ์๊ธ ์์ ์ Commit
GitHub - Kanmanemone/nutri-capture-new
Contribute to Kanmanemone/nutri-capture-new development by creating an account on GitHub.
github.com
#4-3 ๋ณธ ํ๋ก์ ํธ์ ๊ฐ์ฅ ์ต์ Commit
GitHub - Kanmanemone/nutri-capture-new
Contribute to Kanmanemone/nutri-capture-new development by creating an account on GitHub.
github.com
'๊ฐ๋ฐ ์ผ์ง ๐ป > Swemo' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Nutri Capture - NavigationBar ์ปดํฌ๋ํธ (1) | 2024.09.28 |
|---|---|
| Nutri Capture - NavHost (1) | 2024.09.26 |
| Nutri Capture - ์ด์๋จ๋ ์ฑ์ด ๋๋ ค๋ฉด (0) | 2024.09.22 |
| Nutri Capture - UI ์ค์ผ์น (0) | 2023.12.23 |
| Nutri Capture - ERD (0) | 2023.12.22 |