#1 ๊ฐ์
androidx.compose.material3 | Android Developers
androidx.compose.desktop.ui.tooling.preview
developer.android.com
Surface๋ Composable๋ค์ ์ปจํ ์ด๋๋ก, Composable์ ์งํฉ์ ์๊ฐ์ ์ผ๋ก ํํํ๊ธฐ ์ฝ๊ฒ ๋์์ค๋ค.
#2 ๊ตฌ์กฐ
@Composable
@ComposableInferredTarget
public fun Surface(
modifier: Modifier, // Surface์ ์ ์ฉ๋ Modifier
shape: Shape, // Surface์ ๋ชจ์ ์ ์
color: Color, // ๋ฐฐ๊ฒฝ์
contentColor: Color, // Surface ์ Composable๋ค์๊ฒ ์ ์ฉ๋ ๊ธฐ๋ณธ ์ ์ผ๊ด ์ง์ .
tonalElevation: Dp, // ์์ ํค(tone)์ ์ํ ์๊ฐ์ ๊ณ์ธตํ(Elevation)
shadowElevation: Dp, // ๊ทธ๋ฆผ์์ ํฌ๊ธฐ์ ์ํ ์๊ฐ์ ๊ณ์ธตํ(Elevation)
border: BorderStroke?, // ์ฐ๋ฆฌ๊ฐ ์ ์๋ ๊ทธ border
content: @Composable () -> Unit
): Unit
Surface์ ๊ตฌ์กฐ๋ค. ๊ฐ ๋งค๊ฐ๋ณ์์ ๋ํ ์ค๋ช ์ ๋ค์๊ณผ ๊ฐ๋ค.
modifier
Modifier๋ฅผ ์ง์ ํ๋ค.
shape
RoundedCornerShape, CircleShape ๋ฑ์ผ๋ก Surface์ ๋ชจ์์ ์ง์ ํ๋ค.
color
๋ฐฐ๊ฒฝ์์ด๋ค.
contentColor
Surface ์์ ์๋ content๋ค์ ์์ ๋ช ์ํ์ง ์์ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ฉ๋ ์์ด๋ค.
tonalElevation
Surface ๋ผ๋ฆฌ ์ค์ฒฉ๋ ๊ฒฝ์ฐ ์(tone)์ ํตํด ์ค์ฒฉ๋ Surface ๊ณ์ธต์ ์๊ฐํํ๋ค. ํ์ง๋ง ์กฐ๊ฑด์ด ์๋๋ฐ ๋ฐ๋ก, ๊ฐ์ฅ ์์์ ์กด์ฌํ๋ Surface์ color ์์ฑ์ด ColorScheme.surface๋ก ์ง์ ๋์ด ์์ด์ผ ํ๋ค. #3 ๋ฐ #4๋ฅผ ๋ณด๋ฉด ์ดํด๊ฐ ์ฝ๋ค.
shadowElevation
Surface์ ๊ทธ๋ฆผ์ ํจ๊ณผ๋ฅผ ๋ถ์ฌํ๋ค.
border
ํ ๋๋ฆฌ๋ฅผ ์ ์ํ๋ค.
content
Surface ๋ด๋ถ์ ์์๋ค์ด๋ค. ๋๋ค ํํ์(์ด ๊ธ์ #2-5 ์ฐธ์กฐ) ํํ๋ก, Surface( ... ) { ... }์ ๊ตฌ์กฐ์์ ์ค๊ดํธ ๋ถ๋ถ์ด ๋ฐ๋ก ์ด content๋ค.
Surface์ ๋ชจ๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๋ถ ์ด์ฉํ ํ๋ก์ ํธ๋ฅผ ์๋์์ ๋ง๋ค์ด๋ณธ๋ค.
#3 3๊ฐ์ Surface๋ฅผ ์ค์ฒฉํ ์ฝ๋
// package com.example.surface
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.LocalAbsoluteTonalElevation
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.surface.ui.theme.SurfaceTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SurfaceTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background, // ColorScheme์ '์์ ๋ชจ์'์ด๋ค.
tonalElevation = 0.dp, // <- ์ด๊ฒ ๊ธฐ๋ณธ๊ฐ์ด๋ผ ์๋ตํด๋ ๋จ
border = BorderStroke(8.dp, Color.Red)
) {
Box(
modifier = Modifier.fillMaxSize(), // <- ์ด๊ฒ ๊ธฐ๋ณธ๊ฐ์ด๋ผ ์๋ตํด๋ ๋จ
contentAlignment = Alignment.Center
) {
Surface(
modifier = Modifier.fillMaxSize(0.8f),
shape = RoundedCornerShape(36.dp),
tonalElevation = LocalAbsoluteTonalElevation.current + 16.dp,
shadowElevation = 12.dp,
) {
Box(
modifier = Modifier.fillMaxSize(), // <- ์ด๊ฒ ๊ธฐ๋ณธ๊ฐ์ด๋ผ ์๋ตํด๋ ๋จ
contentAlignment = Alignment.Center
) {
Surface(
modifier = Modifier.fillMaxSize(0.8f),
tonalElevation = LocalAbsoluteTonalElevation.current + 16.dp,
contentColor = Color.Blue
) {
Greeting("Android")
}
}
}
}
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
fontSize = 24.sp,
modifier = modifier,
)
}
3๊ฐ์ Surface๋ฅผ ๊ณ์ธต ๊ตฌ์กฐ๋ก ์ค์ฒฉ์์ผฐ๋ค. ๊ทธ๋ฆฌ๊ณ #2์ ๊ฐ์ข ์์ฑ๋ค์ ์ ์ฉ์์ผฐ๋ค.
#4 ์๋ ํ์ธ

tonalElevation์ ๋ผ์ดํธ๋ชจ๋์์๋ ์ ์ ์ด๋์์ง๊ณ , ๋คํฌ๋ชจ๋์์๋ ์ ์ ๋ฐ์์ง๋ค.
#5 ์์ฝ
Surface๋ Composable๋ค์ Container๋ค.
#6 ์์ฑ๋ ์ฑ
android-practice/jetpack-compose/Surface at master ยท Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Jetpack Compose - Layout, Arrangement, Alignment (0) | 2024.07.22 |
---|---|
[Android] Jetpack Compose - Modifier (0) | 2024.07.22 |
[Android] Jetpack Compose - ๊ธฐ์ด (0) | 2024.07.22 |
[Android] Dagger2 - Hilt๋ก ๋ง์ด๊ทธ๋ ์ด์ (0) | 2024.07.12 |
[Android] Data Binding - View Binding (1) | 2024.07.10 |