当前位置:网站首页>清理空的 Jetpack Compose 应用程序模板
清理空的 Jetpack Compose 应用程序模板
2022-08-10 15:37:00 【不秃头de程序猿】
对于纯 Jetpack Compose 应用程序,从技术上讲,我们不再需要用于 Android View 系统的themes.xmland 。colors.xml
但是,我已经看到很多代码仍然具有这些themes.xml并已colors.xml定义。就个人而言,我会删除它们以避免重复或混淆。
1.删除themes.xml和colors.xml
删除themes.xml并colors.xml可能导致编译错误,因为它仍然从AndroidManifest.xml. 你需要修改AndroidManifest.xml
android:theme="@style/Theme.EmptyComposeApp"在两者中删除和
<application
...
android:theme="@style/Theme.EmptyComposeApp">
<activity
...
android:theme="@style/Theme.EmptyComposeApp">
...
</activity>
</application>
替换为android:theme="@android:style/Theme.Material.Light.NoActionBar"in which 是您的themes.xml.
<application
...
android:theme="@android:style/Theme.Material.Light.NoActionBar">
...
</application>
创建一个没有顶部操作栏的应用程序。可以使用Scaffold可组合功能创建顶部操作栏。在这个简单的 LazyColumn 应用程序中查看这个添加顶部应用程序栏示例。
我确实尝试不添加它android:theme=“@android:style/Theme.Material.Light.NoActionBar”,但ComponentActivity()默认情况下会创建顶部操作栏,我不知道使用 Jetpack Compose 摆脱它。
2. 使用 Compose 设置状态栏颜色
好吧,该应用程序可以运行,但是状态栏中的默认紫色消失了。
如果您查看原始文件themes.xml,它会在那里设置状态栏颜色。
<resources>
<style name="Theme.EmptyComposeApp" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:statusBarColor">@color/purple_700</item>
</style>
</resources>
由于这已被删除,我们需要在 Jetpack Compose 中实现它。为此,我们需要使用 Accompanist 的System UI Controller。
添加com.google.accompanist:accompanist-systemuicontroller库:
dependencies {
...
implementation "com.google.accompanist:accompanist-systemuicontroller:0.24.1-alpha"
...
}
在ui\Theme\Theme.kt中,添加这个来设置purple_700颜色,这也是primaryVariant颜色
val systemUiController = rememberSystemUiController()
systemUiController.setStatusBarColor(
color = colors.primaryVariant
)
完整的代码如下所示:
fun EmptyComposeAppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}
val systemUiController = rememberSystemUiController()
systemUiController.setStatusBarColor(
color = colors.primaryVariant
)
MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes,
content = content
)
}
Android Studio 的模板项目没有设置导航栏颜色。因此,我们没有在上面的示例中设置它们。但是,我们通常希望为导航栏和状态栏设置相同的颜色。在这种情况下,您可以使用systemUiController.setSystemBarsColor():
val systemUiController = rememberSystemUiController()
systemUiController.setSystemBarsColor(
color = colors.primaryVariant
)
由于我想将此示例用作我自己的模板项目。我将更改它以systemUiController.setSystemBarsColor()在此应用示例中使用。
预览未渲染
使用系统 UI 控制器时存在预览无法正常工作的错误。在此处查看问题跟踪器。
java.lang.IllegalArgumentException: The Compose View must be hosted in an Activity with a Window!
要解决此问题,您需要通过传入useSystemUIController参数来禁用预览中的系统 UI 控制器。
在Theme.kt:
@Composable
fun EmptyComposeAppTheme(
...
useSystemUIController: Boolean = true,
...
) {
...
if (useSystemUIController) {
val systemUiController = rememberSystemUiController()
systemUiController.setStatusBarColor(
color = colors.primaryVariant)
}
...
}
在MainActivity.kt:
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
EmptyComposeAppTheme(useSystemUIController = false) {
...
}
}
概括
现在我只有strings.xml在我的资源文件夹中。酷,不是吗?
我可能还会将此作为任何 Jetpack Compose 项目的起点。
最后
由于篇幅有限,这里以图片的形式给大家展示一小部分,需要Android jetpack的可以点击文末卡片免费领取。
边栏推荐
- PYSPARK ON YARN报错集合
- metaForce佛萨奇2.0系统开发功能逻辑介绍
- LeetCode-101. Symmetric Tree
- const修饰的指针变量(详解)
- Introduction to the functional logic of metaForce Fosage 2.0 system development
- Zijin Example
- Programmer = overtime??- Master the time to master the life
- IPC:Interrupts and Signals
- TCP为什么是三次握手和四次挥手?
- An ABAP tool that can print the browsing history of a user in the system for BSP applications
猜你喜欢

fuse简介

FP6378AS5CTR SOT - 23-5 effective 1 mhz2a synchronous buck regulator

秒杀项目收获

企业如何开展ERP数据治理工作?_光点科技

Parse the value of uuid using ABAP regular expressions

An ABAP tool that can print the browsing history of a user in the system for BSP applications

产品说明丨如何使用MobPush快速创建应用

Cesium Quick Start 4-Polylines primitive usage explanation

【教程】HuggingFace的Optimum组件已支持加速Graphcore和英特尔Habana芯片

数据在内存中的存储
随机推荐
[Data warehouse design] Why should enterprise data warehouses be layered?(six benefits)
scala basics
Chapter one module of the re module,
关于async\await 的理解和思考
推荐几款最好用的MySQL开源客户端,建议收藏!
Mobileye joins hands with Krypton to open a new chapter in advanced driver assistance through OTA upgrade
颜色空间
Detailed understanding of anonymous functions and all built-in functions (Part 2)
快速申请代码签名证书方法
架构设计之一——基础架构
5G NR MIB详解
【教程】HuggingFace的Optimum组件已支持加速Graphcore和英特尔Habana芯片
metaForce佛萨奇2.0系统开发功能逻辑介绍
超越神经缩放法则:通过数据剪枝
HUAWEI CLOUD DevCloud received the highest-level certification of the first batch of cloud-native technology architecture maturity assessments by the China Academy of Information and Communications Te
Custom picker scroll selector style
dedecms支持Word内容自动导入
WinUI 3 Fundamentals 5小时教学视频
Parse the value of uuid using ABAP regular expressions
openpyxl绘制堆叠图