Android Studio Flamingo/Giraffe中找不到Empty Compose Activity模板求助
解决Empty Compose Activity模板不显示的问题
检查Jetpack Compose插件状态
打开Settings(Windows/Linux:File > Settings;Mac:Android Studio > Settings),进入Plugins,搜索Jetpack Compose,确保插件已启用且是最新版本。未安装则点击安装后重启Android Studio。确认项目创建配置
创建新项目时,Minimum SDK需选择API 21及以上版本,Language必须选Kotlin——这两个是Compose模板加载的基础要求。清理Android Studio缓存
点击File > Invalidate Caches...,选择Invalidate and Restart,等待Studio重启并重建缓存,多数模板加载异常都能通过这个操作解决。手动搭建Empty Compose Activity
如果以上方法无效,可手动构建项目结构:- 创建普通的Empty Activity项目(选择Kotlin语言)
- 在模块级
build.gradle中添加Compose依赖:dependencies { implementation platform('androidx.compose:compose-bom:2023.06.01') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' 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' } - 在
build.gradle的android块中启用Compose:android { buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.4.7' } } - 将默认Activity代码替换为Compose实现:
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MyApplicationTheme { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { Greeting("Android") } } } } } @Composable fun Greeting(name: String, modifier: Modifier = Modifier) { Text( text = "Hello $name!", modifier = modifier ) } @Preview(showBackground = true) @Composable fun GreetingPreview() { MyApplicationTheme { Greeting("Android") } } - 同步项目后即可运行。
内容的提问来源于stack exchange,提问作者Edenilson Ferreira
相关产品推荐
相关产品推荐

