enableEdgeToEdge 函数
@JvmName("enable")
@JvmOverloads
fun ComponentActivity.enableEdgeToEdge(
statusBarStyle: SystemBarStyle = SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT),
navigationBarStyle: SystemBarStyle = SystemBarStyle.auto(DefaultLightScrim, DefaultDarkScrim)
) {
val view = window.decorView
val statusBarIsDark = statusBarStyle.detectDarkMode(view.resources)
val navigationBarIsDark = navigationBarStyle.detectDarkMode(view.resources)
val impl = Impl ?: if (Build.VERSION.SDK_INT >= 29) {
EdgeToEdgeApi29()
} else if (Build.VERSION.SDK_INT >= 26) {
EdgeToEdgeApi26()
} else if (Build.VERSION.SDK_INT >= 23) {
EdgeToEdgeApi23()
} else if (Build.VERSION.SDK_INT >= 21) {
EdgeToEdgeApi21()
} else {
EdgeToEdgeBase()
}.also { Impl = it }
impl.setUp(
statusBarStyle, navigationBarStyle, window, view, statusBarIsDark, navigationBarIsDark
)
}
- 在调用 enableEdgeToEdge 函数后,系统状态栏和导航栏会变为透明或半透明,使应用内容可以在其后方显示
MaterialTheme 函数
@Suppress("DEPRECATION_ERROR")
@Composable
fun MaterialTheme(
colorScheme: ColorScheme = MaterialTheme.colorScheme,
shapes: Shapes = MaterialTheme.shapes,
typography: Typography = MaterialTheme.typography,
content: @Composable () -> Unit
) {
val rippleIndication = androidx.compose.material.ripple.rememberRipple()
val selectionColors = rememberTextSelectionColors(colorScheme)
CompositionLocalProvider(
LocalColorScheme provides colorScheme,
LocalIndication provides rippleIndication,
androidx.compose.material.ripple.LocalRippleTheme provides MaterialRippleTheme,
LocalShapes provides shapes,
LocalTextSelectionColors provides selectionColors,
LocalTypography provides typography,
) {
ProvideTextStyle(value = typography.bodyLarge, content = content)
}
}
| 参数 |
说明 |
colorScheme: ColorScheme |
主题颜色配置 |
shapes: Shapes |
主题形状配置 |
typography: Typography |
主题排版配置 |
content: @Composable () -> Unit |
Compose 函数类型参数,可以把它理解为一块 UI 内容 例如,传入某个页面的 UI 内容,这块 UI 内容就会自动继承当前 MaterialTheme 样式 Unit 表示这个函数没有实际的返回值 |
remember 函数
1、基本介绍
- remember 是一个 Composable 函数,用于在 Composable 函数重组时记住一个值
2、演示
- 无参数的 remember 函数
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text("Count: $count")
}
- 带 key 的 remember
var name by remember { mutableStateOf("tom") }
val content = remember(name) {
"Hello, $name!"
}
Button(onClick = {
if (name == "tom") {
name = "jerry"
} else if (name == "jerry") {
name = "tom"
}
}, content = {
Text(content)
})
var name by remember { mutableStateOf("tom") }
var age by remember { mutableStateOf(18) }
val content = remember(name, age) {
"Hello, $name! You are $age years old."
}
Button(onClick = {
name = "jerry"
age = 20
}, content = {
Text(content)
})
3、数据类型状态
- 基础类型状态
var inputText by remember { mutableStateOf("") }
Column {
TextField(
value = inputText,
onValueChange = { inputText = it },
label = { Text(text = "请输入内容") }
)
Text("你输入的内容:$inputText")
}
var isChecked by remember { mutableStateOf(false) }
Column {
Switch(
checked = isChecked,
onCheckedChange = { isChecked = it }
)
Text("开关状态:${if (isChecked) "开启" else "关闭"}")
}
- 自定义数据类状态,支持存储自定义数据类,修改时必须赋值新的对象,否则 Compose 无法感知状态变化,因为对象引用未改变
data class User(
val id: Int,
val name: String,
val age: Int
)
var user by remember {
mutableStateOf(User(id = 1, name = "张三", age = 20))
}
Column {
Text("用户ID:${user.id}")
Text("用户姓名:${user.name}")
Text("用户年龄:${user.age}")
Button(onClick = {
user = user.copy(age = user.age + 1)
}) {
Text("年龄 + 1")
}
}
- 集合类型状态,支持存储集合类型,修改时必须赋值新的集合对象,否则 Compose 无法感知状态变化,因为对象引用未改变
var fruitList by remember {
mutableStateOf(listOf("苹果", "香蕉", "橙子"))
}
Column {
fruitList.forEach { fruit ->
Text(text = fruit)
}
Button(onClick = {
fruitList = fruitList + "葡萄"
}) {
Text("添加葡萄")
}
}
补充学习
1、尾随 Lambda
(1)基本介绍
- 当一个函数的最后一个参数是函数类型时,并使用 Lambda 表达式作为参数传递时,可以将 Lambda 表达式放在括号外
(2)演示
fun say_hello(name: String, other_operation: () -> Unit): Unit {
println("你好,$name")
other_operation()
}
say_hello("张三", {
println("今天的天气不错")
println("比较适合去旅游")
})
say_hello("李四") {
println("今天的天气不错")
println("比较适合去旅游")
}
# 输出结果
你好,张三
今天的天气不错
比较适合去旅游
你好,李四
今天的天气不错
比较适合去旅游
fun test(operation: () -> Unit): Unit {
println("测试开始")
operation()
println("测试结束")
}
test {
println("测试操作...")
}
# 输出结果
测试开始
测试操作...
测试结束
fun custom_compute(operation: (Int, Int) -> Int): Unit {
println("计算开始")
val result = operation(10, 20)
println("计算结果:$result")
}
custom_compute { a, b ->
a + b
}
# 输出结果
计算开始
计算结果:30
传递函数类型参数
- 使用 Lambda 表达式
fun custom_compute(operation: (Int, Int) -> Int): Unit {
println("计算开始")
val result = operation(10, 20)
println("计算结果:$result")
}
custom_compute { a, b ->
a + b
}
# 输出结果
计算开始
计算结果:30
- 使用函数引用
fun custom_compute(operation: (Int, Int) -> Int): Unit {
println("计算开始")
val result = operation(10, 20)
println("计算结果:$result")
}
fun custom_compute_operation(a: Int, b: Int): Int {
return a * b
}
custom_compute(::custom_compute_operation)
# 输出结果
计算开始
计算结果:200
- 使用匿名函数
fun custom_compute(operation: (Int, Int) -> Int): Unit {
println("计算开始")
val result = operation(10, 20)
println("计算结果:$result")
}
custom_compute(fun(a: Int, b: Int): Int {
return a - b
})
# 输出结果
计算开始
计算结果:-10