android - 设置不同的主题
问题描述
我想使用一些主题,例如 Dark 和 Light。我在 style.xml 中编写了两种不同的样式:DarkStyle 和 LightStyle。我已经通过 color.xml 设置了部分易失性属性,并立即设置了部分。
<style name="BaseTheme.LightStyle">
<item name="clientBrand">@color/clientBrand</item>
<item name="desktopScreenBg">#E6E6E6</item>
</style>
<style name="BaseTheme.DarkStyle">
<item name="clientBrand">@color/clientBrand</item>
<item name="desktopScreenBg">#000000</item>
</style>
- 这是选择主题的最佳实践
。建造时间?
湾。运行? - 我如何从我的主题中设置一些其他主题值。
<style name="OtherTheme">
<item name="android:textColor">dependsOnChosenTheme</item>
</style>
最终目标是:
<ViewGroup background = DarkOrLightColorBg/>
<TextView textColor = DarkOrLightColorBg/>
我想出了如何在构建时绑定所需的主题
<style name="LightStyle">
<style name="DarkStyle">
//by requrements
<style name="DarkStyle.ActiveStyle">
现在,我怎么能做这样的事情?
<ViewGroup backgroun=ActiveStyle.colorBg/>
//Or
<style SomeTheme
<item name="android:textColor">@style/ActiveStile.colorText</item>
</style>
解决方案
这是一个如何创建多主题应用程序的示例。这很简单,您的活动有一个上下文,在上下文中您可以覆盖方法以按用户注入选定的主题。
此外,如果您不需要运行时更改,您可以在编译时更改主题,在 AndroidManifest.xml 中。
推荐阅读
- python - 如何在 lambda.apply 函数中应用嵌套 for 循环来迭代数据帧中的列,下图中的预期输出
- asp.net - 从(本地)Windows 身份验证更改为 ADFS
- asp.net-core - .Net Core / Identity Server - 是否可以允许匿名但只能来自我的客户?
- flutter - Flutter Provider:拆分通知的正确方法
- javascript - React Native - 我无法从我的 FETCH 中得到响应,但我的 api 确实得到了我发布的数据
- ios - iOS CoreMotion.MotionThread EXC_BAD_ACCESS 在 stopDeviceMotionUpdates() 被调用后被抛出
- css - CSS 表格样式仅适用于特定单元格,但会覆盖其他单元格
- powershell - 从 .txt 文件中获取 -Filter 或 -Include 值
- java - 卡夫卡给出:“在实际进入消费者组之前,组成员需要有一个有效的成员ID”
- javascript - 创建一个可以存储共享实例的 npm 模块