当然!下面是一个简单的 Jetpack Compose 示例项目,它展示了一个基本的用户界面,包括一个按钮和一段可变的文本。当用户点击按钮时,文本将更新。
项目结构
假设你已经在 Android Studio 中创建了一个新的 Jetpack Compose 项目。下面的代码可以替换 MainActivity.kt 文件的内容。
MainActivity.kt
package com.example.composeexample
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.* // 使用 material3
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.composeexample.ui.theme.ComposeExampleTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeExampleTheme { // 使用主题
// 一个 Surface 容器,设置背景颜色
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
GreetingScreen()
}
}
}
}
}
@Composable
fun GreetingScreen() {
// 使用 remember 和 mutableStateOf 来保存和更新状态
var buttonClicked by remember { mutableStateOf(false) }
// 使用 Column 进行垂直布局
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp), // 内边距
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = if (buttonClicked) "按钮已点击!" else "欢迎使用 Jetpack Compose!",
style = MaterialTheme.typography.headlineMedium
)
Spacer(modifier = Modifier.height(24.dp)) // 间隔
Button(onClick = { buttonClicked = true }) {
Text(text = "点击我")
}
}
}
主题文件
确保你的 ui.theme 包含必要的主题设置。如果你是使用 Android Studio 创建的 Compose 项目,这些文件通常已经包含。如果需要,你可以查看或修改 Theme.kt 文件。例如:
package com.example.composeexample.ui.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
private val LightColors = lightColorScheme(
primary = /* 你的颜色 */,
// 其他颜色
)
private val DarkColors = darkColorScheme(
primary = /* 你的颜色 */,
// 其他颜色
)
@Composable
fun ComposeExampleTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
val colors = if(darkTheme) DarkColors else LightColors
MaterialTheme(
colorScheme = colors,
typography = Typography,
content = content
)
}
依赖项
确保在 build.gradle (app 模块) 中包含必要的 Jetpack Compose 依赖项。例如:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.example.composeexample"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
// ...
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.4.3' // 确保使用最新版本
}
// ...
}
dependencies {
implementation "androidx.core:core-ktx:1.10.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1"
implementation "androidx.activity:activity-compose:1.7.2"
implementation platform("androidx.compose:compose-bom:2023.10.00")
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation "androidx.compose.material3:material3"
debugImplementation "androidx.compose.ui:ui-tooling"
debugImplementation "androidx.compose.ui:ui-test-manifest"
// 其他依赖
}
运行效果
当你运行这个应用时,屏幕中央会显示一段文本“欢迎使用 Jetpack Compose!”和一个“点击我”的按钮。当你点击按钮后,文本会更新为“按钮已点击!”。
进一步扩展
这个示例是一个基础入门,你可以根据需求进一步扩展功能,例如:
- 添加更多的交互组件(如 TextField、Checkbox 等)。
- 使用
LazyColumn 展示列表数据。
- 引入导航组件进行多屏导航。
- 集成 ViewModel 进行更复杂的状态管理。
参考资料
希望这个示例对你入门 Jetpack Compose 有所帮助!如果有任何疑问,请随时提问。