首页 > 解决方案 > 设置不同的主题

问题描述

我想使用一些主题,例如 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>
  1. 这是选择主题的最佳实践
    。建造时间?
    湾。运行?
  2. 我如何从我的主题中设置一些其他主题值。

<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>

标签: androidthemes

解决方案


这是一个如何创建多主题应用程序的示例。这很简单,您的活动有一个上下文,在上下文中您可以覆盖方法以按用户注入选定的主题。

此外,如果您不需要运行时更改,您可以在编译时更改主题,在 AndroidManifest.xml 中。

在此处输入图像描述


推荐阅读