๐ Compose๋ก ๊ตฌํํ ์ปค์คํ ์ปดํฌ๋ํธ์ ๋ฆฌ์คํธ ํ๋ฉด ์ ๋ฆฌ
โ BankComponent
- More ์์ด์ฝ: ์ถ๊ฐ ์ต์ ๋ฒํผ ํ์.
- ์ปค์คํ
๋ฐฐ๊ฒฝ ์์: ํธ์ถ ์ ์์ ์ง์ ๊ฐ๋ฅ.
- ์ปดํฌ์ ๋ธ ํจ์ ์์ฑ ๋ฐฉ์
- @Composable ์ ๋ ธํ ์ด์ ์ ํตํด UI ์ปดํฌ๋ํธ๋ฅผ ์ ์ธ.
- ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋๋ ๊ธฐ๋ณธ ํจํด์ ๋ฐฐ์ธ ์ ์์.
- ๋ ์ด์์ ๊ตฌ์ฑ ๋ฐ ์ ๋ ฌ
- Box๋ฅผ ํ์ฉํ์ฌ ์ฌ๋ฌ UI ์์๋ฅผ ๊ฒน์น๊ฑฐ๋ ํน์ ์์น์ ๋ฐฐ์นํ๋ ๋ฐฉ์์ ๋ฐฐ์ธ ์ ์์.
- Modifier.align()์ ํตํด Alignment ์ต์ ์ผ๋ก ์์น๋ฅผ ์กฐ์ .
- UI ์ปค์คํฐ๋ง์ด์ง
- ์ ๋ ฅ๊ฐ: name, amount, backgroundColor๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ๋์ ์ผ๋ก UI๋ฅผ ๋ณ๊ฒฝ.
- ๊ธฐ๋ณธ๊ฐ ์ ๊ณต: backgroundColor์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ์ฌ ์ ์ฐ์ฑ์ ๋์.
BankComponent ์ฝ๋
// src/main/java/com/gdg/kakaobank/component/Bank.kt
package com.gdg.kakaobank.component
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.gdg.kakaobank.R
import com.gdg.kakaobank.ui.theme.B4_R
import com.gdg.kakaobank.ui.theme.Black
import com.gdg.kakaobank.ui.theme.H6_B
import com.gdg.kakaobank.ui.theme.Main_Yellow
@Composable
fun BankComponent(name: String, amount: String, backgroundColor: Color = Main_Yellow) {
Box(
modifier = Modifier
.size(340.dp, 140.dp)
.background(backgroundColor, shape = RoundedCornerShape(20.dp))
.padding(16.dp)
) {
Image(
painter = painterResource(id = R.drawable.ic_bank),
contentDescription = "KaKao Bank Icon",
contentScale = ContentScale.Fit,
modifier = Modifier
.size(31.dp)
.align(Alignment.TopStart)
.offset(y = 10.dp) // ์๋๋ก ์ด๋
)
Text(
text = "${name}์ ํต์ฅ",
style = B4_R,
color = Black,
modifier = Modifier
.align(Alignment.TopStart)
.padding(start = 40.dp)
.offset(y = 10.dp) // ์๋๋ก ์ด๋
)
Text(
text = "${amount}์",
style = H6_B,
color = Black,
modifier = Modifier
.align(Alignment.TopStart)
.padding(start = 40.dp)
.offset(y = 26.dp) // ์๋๋ก ์ด๋
)
Image(
painter = painterResource(id = R.drawable.more),
contentDescription = "More Icon",
contentScale = ContentScale.Fit,
modifier = Modifier
.size(29.dp)
.align(Alignment.TopEnd)
)
}
}
โ HomeScreen
HomeScreen์ ๊ณ์ข ์ ๋ณด๋ฅผ ํ์ํ๋ ๋ฉ์ธ ํ๋ฉด์ ๋๋ค.
- ์ฃผ์ ๊ธฐ๋ฅ
- LazyColumn: ๋ฆฌ์คํธ ๊ธฐ๋ฐ ๊ณ์ข ์ ๋ณด๋ฅผ ํจ์จ์ ์ผ๋ก ๋ ๋๋ง.
- ๋ค์ํ ๋ฐ์ดํฐ ๊ด๋ฆฌ: bankList๋ฅผ ํ์ฉํด ๋ค์์ ๊ณ์ข ์ ๋ณด๋ฅผ ๋์ ์ผ๋ก ์์ฑ.
- ์คํ์ผ๋ง ๋ฐ ์ ๋ ฌ: ์๋จ ํค๋์ ๊ณ์ข ๋ฆฌ์คํธ๋ฅผ ๊ตฌ๋ถํด ๋ฐฐ์น.
- ๊ตฌํ ํน์ง
- BankComponent๋ฅผ ํ์ฉํ์ฌ ๋ฆฌ์คํธ ๊ตฌ์ฑ.
- ๊ณ์ข ๋ณ๋ก ๋ค๋ฅธ ๋ฐฐ๊ฒฝ์์ ์ค์ ํ๊ธฐ ์ํด ์์ ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋์ ์คํ์ผ๋ง ๊ตฌํ.
HomeScreen ์ฝ๋
@Composable
fun HomeScreen() {
val bankList = listOf(
Pair("๊น๋ํ", "700,000,000"),
Pair("์กฐ์์", "700,000,000"),
Pair("์ด๊ฐ์", "100,000,000"),
Pair("๋ฐฑ์์ฐ", "100,000,000"),
Pair("์ดํ์ง", "100,000,000")
)
Column(
modifier = Modifier
.fillMaxSize()
.background(White)
.padding(top = 24.dp)
) {
// ์๋จ ํค๋
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(text = "๋์ก์ด", style = H5_B, color = Black)
Spacer(modifier = Modifier.width(8.dp))
Text(text = "๋ด ๊ณ์ข", style = B4_B, color = Black)
}
Image(
painter = painterResource(id = R.drawable.bell),
contentDescription = "Bell Icon",
modifier = Modifier.size(30.dp)
)
}
Spacer(modifier = Modifier.height(20.dp))
// ๊ณ์ข ๋ฆฌ์คํธ
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.background(White),
horizontalAlignment = Alignment.CenterHorizontally
) {
// ๋ด ๊ณ์ข ํ์
items(1) {
MyBankComponent(name = "๋์ก์ด", amount = "100,000,000")
Spacer(modifier = Modifier.height(12.dp))
}
// ๋ค๋ฅธ ์ฌ์ฉ์ ๊ณ์ข ํ์
items(bankList.size) { index ->
val colors = listOf(Pink, Dark_Mint, Light_Mint, Deep_Blue)
val (name, amount) = bankList[index]
BankComponent(
name = name,
amount = amount,
backgroundColor = colors[index % colors.size]
)
Spacer(modifier = Modifier.height(12.dp))
}
}
}
}
๋ก๊ทธ์์ ๋ค์ด์ผ๋ก๊ทธ ๊ตฌํ
๋ก๊ทธ์์ ๋ฐ ํ์ ํํด ๊ธฐ๋ฅ์ ํ์ ๋ค์ด์ผ๋ก๊ทธ๋ก ๊ตฌํ๋์ด ์ฌ์ฉ์์ ์์ฌ๋ฅผ ๋ช ํํ ํ์ธํ ์ ์๋๋ก ๋์์ธํ์ต๋๋ค.
โ ์ฃผ์ ๊ตฌํ ์์
- ๋ค์ด์ผ๋ก๊ทธ ์ฐฝ: AlertDialog๋ฅผ ํ์ฉํด ์ฌ์ฉ์ ํ์ธ ์ ์ฐจ ์ถ๊ฐ.
- Toast ๋ฉ์์ง: ๋์ ์๋ฃ ์ ํผ๋๋ฐฑ ์ ๊ณต.
- ์ ์ฐํ UI: DialogProperties๋ฅผ ํตํด ํ๋ซํผ์ ๋ง๋ ๋ค์ด์ผ๋ก๊ทธ ํฌ๊ธฐ ์ค์ .
MoreScreen ์ฝ๋
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.gdg.kakaobank.R
import com.gdg.kakaobank.ui.theme.*
import androidx.compose.ui.window.DialogProperties
import androidx.compose.foundation.shape.RoundedCornerShape
import android.widget.Toast
import androidx.compose.ui.platform.LocalContext
@Composable
fun MoreScreen() {
val moreInfoList1 = listOf("์๋น์ค ์ด์ฉ์ฝ๊ด", "๊ฐ์ธ์ ๋ณด ์ฒ๋ฆฌ๋ฐฉ์นจ", "๋ฒ์ ์ ๋ณด")
val moreInfoList2 = listOf("๊ณ ๊ฐ์ผํฐ", "๋ก๊ทธ์์", "ํ์ ํํด")
var showDialog by remember { mutableStateOf(false) }
var dialogType by remember { mutableStateOf("") }
val context = LocalContext.current
Column(
modifier = Modifier
.fillMaxSize()
.background(White)
.padding(top = 45.dp, start = 16.dp, end = 30.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Column(
modifier = Modifier
.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(5.dp) //์ค๊ฐ๊ฒฉ ์์ฑ
) {
Text("๋์ก์ด๋", style = H5_B)
Text("์นด์นด์ค๋ฑ
ํฌ ์๋น์ค", style = H5_B, color = Main_Yellow)
Text("๊ด๋ จ ์๋ด์ฌํญ์
๋๋ค", style = H5_B)
}
Spacer(modifier = Modifier.height(16.dp))
Text("์ด์ฉ์๋ด", style = H6_SB)
Spacer(modifier = Modifier.height(4.dp))
moreInfoList1.forEach { info ->
MoreInfo(text = info)
}
Spacer(modifier = Modifier.height(16.dp))
Text("๊ธฐํ", style = H6_SB)
Spacer(modifier = Modifier.height(4.dp))
//ํ์ ํํด ๋ฐ ๋ก๊ทธ์์ ํ์
์๋ฆผ ๋จ๋๋ก ์ค์
moreInfoList2.forEach { info ->
if (info == "ํ์ ํํด" || info == "๋ก๊ทธ์์") {
MoreInfo(text = info, onClick = {
showDialog = true
dialogType = info
})
} else {
MoreInfo(text = info)
}
}
}
if (showDialog) {
AlertDialog(
onDismissRequest = { showDialog = false },
text = {
Text(
"${dialogType} ํ์๊ฒ ์ต๋๊น?",
style = H5_SB,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 30.dp, 16.dp, 10.dp)
)
},
confirmButton = {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly
) {
Button(
colors = ButtonDefaults.buttonColors(containerColor = Gray, contentColor = White),
onClick = {
showDialog = false
},
modifier = Modifier.size(width = 110.dp, height = 45.dp)
) {
Text("์ทจ์", style = H7_B)
}
Button(
colors = ButtonDefaults.buttonColors(containerColor = Main_Yellow, contentColor = Black),
onClick = {
showDialog = false
Toast.makeText(context, "${dialogType} ๋์์ต๋๋ค", Toast.LENGTH_SHORT).show()
},
modifier = Modifier.size(width = 110.dp, height = 45.dp)
) {
Text(dialogType, style = H7_B)
}
}
},
properties = DialogProperties(usePlatformDefaultWidth = false),
containerColor = Light_Gray,
shape = RoundedCornerShape(16.dp),
modifier = Modifier.size(width = 280.dp, height = 180.dp)
)
}
}
@Composable
fun MoreInfo(text: String, onClick: () -> Unit = {}) {
Row(
modifier = Modifier
.fillMaxWidth()
.background(White)
.padding(8.dp)
.clickable(onClick = onClick),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
) {
Text(text, style = B1_SB, color = Dark_Gray)
Image(painter = painterResource(id = R.drawable.icn_next), contentDescription = null)
}
}
@Composable
@Preview(showBackground = true)
fun MoreScreenPreview() {
MoreScreen()
}
๋ค์ด์ผ๋ก๊ทธ ์ฐฝ๊ณผ Toast ๋ฉ์์ง ์ ๋ฆฌ
โ ๋ค์ด์ผ๋ก๊ทธ ์ฐฝ (AlertDialog)
- ๊ธฐ๋ฅ: ์ฌ์ฉ์ ํ์ธ ์ ์ฐจ๋ฅผ ์ถ๊ฐํ์ฌ ์ค์ ๋ฐฉ์ง.
- ๊ตฌํ: AlertDialog๋ฅผ ์ฌ์ฉํ์ฌ ๋ก๊ทธ์์ ๋๋ ํ์ ํํด ์ ํ์ธ ์ฐฝ ํ์.
- ๊ตฌ์ฑ ์์:
- ์ทจ์ ๋ฒํผ: ์์ ์ ์ค๋จํ๊ณ ๋ค์ด์ผ๋ก๊ทธ๋ฅผ ๋ซ์.
- ํ์ธ ๋ฒํผ: ์์ ์ ์ํํ๊ณ ๋ค์ด์ผ๋ก๊ทธ๋ฅผ ๋ซ์.
if (showDialog) {
AlertDialog(
onDismissRequest = { showDialog = false },
text = {
Text(
"${dialogType} ํ์๊ฒ ์ต๋๊น?",
style = H5_SB,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 30.dp, 16.dp, 10.dp)
)
},
confirmButton = {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly
) {
Button(
colors = ButtonDefaults.buttonColors(containerColor = Gray, contentColor = White),
onClick = {
showDialog = false
},
modifier = Modifier.size(width = 110.dp, height = 45.dp)
) {
Text("์ทจ์", style = H7_B)
}
Button(
colors = ButtonDefaults.buttonColors(containerColor = Main_Yellow, contentColor = Black),
onClick = {
showDialog = false
Toast.makeText(context, "${dialogType} ๋์์ต๋๋ค", Toast.LENGTH_SHORT).show()
},
modifier = Modifier.size(width = 110.dp, height = 45.dp)
) {
Text(dialogType, style = H7_B)
}
}
},
properties = DialogProperties(usePlatformDefaultWidth = false),
containerColor = Light_Gray,
shape = RoundedCornerShape(16.dp),
modifier = Modifier.size(width = 280.dp, height = 180.dp)
)
}
โ Toast ๋ฉ์์ง
- ๊ธฐ๋ฅ: ์์ ์๋ฃ ํ ์ฌ์ฉ์์๊ฒ ํผ๋๋ฐฑ ์ ๊ณต.
- ๊ตฌํ: Toast.makeText()๋ฅผ ํตํด ์์ ์ฑ๊ณต ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ.
Toast.makeText(context, "${dialogType} ๋์์ต๋๋ค", Toast.LENGTH_SHORT).show()
'Group Study (2024-2025) > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] ์นด์นด์ค๋ฑ ํฌ ํด๋ก ์ฝ๋ฉ (4) (4) | 2024.12.18 |
---|---|
[Android] ์นด์นด์ค๋ฑ ํฌ ํด๋ก ์ฝ๋ฉ (3) (1) | 2024.11.29 |
[Android] ์นด์นด์ค๋ฑ ํฌ ํด๋ก ์ฝ๋ฉ (0) | 2024.11.20 |
[Android] ์คํ์ผ ๊ฐ์ด๋์ ๋์์ธ ํจํด (3) | 2024.11.13 |
[Android] Preferences Datastore, Room์ ํ์ฉํ ๋ก์ปฌ ๋ฐ์ดํฐ ์ ์ฅ (3) | 2024.11.03 |