简体   繁体   English

在一个主题中定义多个textColor

[英]Define multiple textColor in one theme

Currently I'm trying to define in my application a theme for day mode and another for night mode. 目前,我正在尝试在应用程序中为白天模式定义一个主题,为夜间模式定义另一个。 I can already change between those two, the problem is that I have textViews with white text color and others with black text color. 我已经可以在这两个之间切换了,问题是我有白色文本颜色的textViews和其他黑色文本颜色的textViews。 So I would like to know if there is a way for: 所以我想知道是否有办法:

  • Define multiple textColors in one theme; 在一个主题中定义多个textColors;

    OR 要么

  • Define themes that change the style, eg 定义更改样式的主题,例如
 // defined in the layout <TextView style="@style/whiteStyleText" /> <TextView style="@style/blackStyleText" /> // defined in the styles <style name="Theme.DayTheme" parent="@style/Theme.ActivityTheme"> <item name="@style/whiteStyleText">@color/white</item> <item name="@style/blackStyleText">@color/black</item> </style> <style name="Theme.NightTheme" parent="@style/Theme.ActivityTheme"> <item name="@style/whiteStyleText">@color/light_red</item> <item name="@style/blackStyleText">@color/red</item> </style> 

Many thanks in advance. 提前谢谢了。

this might help you a bit. 这可能会对您有所帮助。 Not a 100% sure what you want. 不能100%确定您想要什么。

It is to define all your colors in one place. 它是在一个地方定义所有颜色。 Place this is res>values>styles.xml 将其放置在res> values> styles.xml

<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<style name="AppTheme" parent="AppBaseTheme">
</style>

<color name="blueDefault">#2782AF</color>
<color name="blueBGDark">#226A87</color>
<color name="blueDark">#257493</color>
<color name="blueLight">#297F99</color>
<color name="greyDefault">#3F4144</color>

</resources>

I haven't found anything about multiple textColors defined in the same theme. 我还没有发现关于在同一主题中定义的多个textColors的任何信息。

One solution , which I've done is, set a default textColor for the dayTheme and another one for the nightTheme. 我已经完成的一种解决方案是,为dayTheme设置默认的textColor,为nightTheme设置另一种。 For all other textViews that does not use that default color, I've defined it in the layout, and at the beginning of the activity I check what is selected, day or night mode and I call setContentView with the proper layout. 对于所有其他不使用该默认颜色的textView,我已经在布局中对其进行了定义,并且在活动开始时,我会检查所选的白天或黑夜模式,并使用正确的布局调用setContentView。

Another solution would be, declare a custom attribute, eg textColorNight, and create a custom textView which would use this attribute and then would check what mode is currently selected, so it could decide if the textColor (day mode) or the textColorNight (night mode) would be applied to it's text. 另一个解决方案是,声明一个自定义属性,例如textColorNight,并创建一个自定义textView,它将使用此属性,然后检查当前选择的模式,以便它可以确定textColor(白天模式)还是textColorNight(夜间模式) )将应用于其文本。 I thing this second solution is a more refined one, but I haven't implemented it because of the lack of time. 我认为第二种解决方案是一种更完善的解决方案,但是由于时间不足,我尚未实现它。 But next time I need to implement the day/night mode, I'll do it this way. 但是下一次我需要实现白天/夜晚模式时,我将采用这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM