📌  相关文章
📜  在 Jetpack Compose Kotlin Android Studio 中重用 Composables - 无论代码示例

📅  最后修改于: 2022-03-11 14:59:50.085000             🧑  作者: Mango

代码示例1
// CommonLayouts.kt

@Composable
fun Header(content: @Composable () -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(Color.Green)
            .padding(horizontal = 8.dp)
    ) {
        content();
    }
}

@Composable
fun Footer(color: Color, message: String) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(color)
            .padding(horizontal = 8.dp)
    ) {
        Box(contentAlignment = Alignment.Center) {
            Text(text = message)
        }
    }
}

/* ------------------------X---------------------------- */

// MainActivity.kt
class MainActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentLayout {
            Header {
                Button(onClick = {/* TODO */}) {
                    Text(text = "Click me!")
                }
            }
            /* More content */
            Footer(Color.Red, "Jetpack Compose")
        }
    }
}