今天閱讀「適用於 Android 開發人員的 Jetpack Compose」第一章「Compose 基礎知識」的「編寫第一個 Compose 應用程式」,今天看 8~14 小章
by
代表屬性委派,這樣就不用輸入.value
val isExpanded = remember { mutableStateOf(false) } //加.value
var isExpanded by remember { mutableStateOf(false) } //不加.value
@Composable
fun OnboardingScreen(
onContinueClicked: () -> Unit, //建立自己命中的函式onContinueClicked
modifier: Modifier = Modifier
) {
Column(
modifier = modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Welcome to the Basics Codelab!")
Button(
modifier = Modifier.padding(vertical = 24.dp),
onClick = onContinueClicked //點擊事件傳遞函式
) {
Text("Continue")
}
}
}
@Composable
fun OnboardingPreview() {
BasicsCodelabTheme {
OnboardingScreen(onContinueClicked = {})
}
}
LazyColumn
做出捲動
androidx.compose.foundation.lazy.items
LazyColumn(modifier = modifier.padding(vertical = 4.dp)) {
items(items = names) { name ->
Greeting(name = name)
}
}
LazyColumn
不會像 RecyclerView
一樣回收子項rememberSaveable
,這樣旋轉畫面、變更為深色模式、上下捲動等操作會保留remember
狀態,這樣旋轉畫面、變更為深色模式、上下捲動等等操作會變成初始頁面animateDpAsState
//直接放大
val isExtraPadding = if (isExpanded) {
48.dp
} else {
0.dp
}
//動畫放大
val isExtraPadding by animateDpAsState(
if (isExpanded) {
48.dp
} else {
0.dp
}
)
animateDpAsState
裡面有animationSpec
擴展,可以增加動畫樣式
val isExtraPadding by animateDpAsState(
if (isExpanded) {
48.dp
} else {
0.dp
},
//彈跳動畫
animationSpec = spring(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness = Spring.StiffnessLow
)
)
copy
Text(
text = name, style = MaterialTheme.typography.headlineMedium.copy(
fontWeight = FontWeight.ExtraBold
)
)
uiMode = UI_MODE_NIGHT_YES
@Preview(
showBackground = true,
widthDp = 320,
uiMode = UI_MODE_NIGHT_YES,
name = "Dark"
)
@Preview(showBackground = true, widthDp = 320)
@Composable
private fun GreetingsPreview() {
BasicsCodelabTheme {
Greetings()
}
}
val Navy = Color(0xFF073042)
val Blue = Color(0xFF4285F4)
val LightBlue = Color(0xFFD7EFFE)
val Chartreuse = Color(0xFFEFF7CF)
DarkColorScheme
或LightColorScheme
修改
private val DarkColorScheme = darkColorScheme(
surface = Blue,
onSurface = Navy,
primary = Navy,
onPrimary = Chartreuse
)
private val LightColorScheme = lightColorScheme(
surface = Blue,
onSurface = Color.White,
primary = LightBlue,
onPrimary = Navy
)
stringResource
<resources>
<string name="is_click">被按下</string>
</resources>
stringResource(R.string.is_click)