2

after searching here for some hours and testing this and that, i could neither solve my problem, nor find any question/answers here that apply to my problem - but maybe i only use the wrong search terms - so i'm thankful for any hints in the right direction!

Basically it's about a app using 2 Themes (Dark & Light), where some TextViews show a status - status can be On or Off, and for better recognition of the state, the color of the text should reflect the state: Red for Off and Green for On.

When i did not use any themes or styles, i simply used the TextView's .setColor(#ffff0000) for the red color, and so on...

then i started to make use of the Dark / Light Themes for better visibility in different situations - e.g. outdoors black text on white background is easier to read.

therefore i've defined my 2 themes, overwriting the textAppearance:

<style name="Theme.MyApp" parent="Theme.Sherlock">
...
<item name="android:textAppearance">@style/TextNormal</item>
...
</style>

<style name="Theme.MyApp.Light" parent="Theme.Sherlock.Light">
...
 <item name="android:textAppearance">@style/TextNormalLight</item>
...

<style name="TextNormal">
    <item name="android:textColor">#ffffffff</item>
</style>

<style name="TextNormalLight">
    <item name="android:textColor">#ff000000</item>
</style>

This works fine, and the "normal" textviews are displayed in the correct color depending on the theme. So far so good!

Instead of using TextView's setColor, i switched to use .setTextAppearance tv.setTextAppearance(getSherlockActivity(), R.style.TextRed);

and defined some styles (only pasting one, the others are TextGreen,...):

<style name="TextRed">
    <item name="android:textColor">#ffff0000</item>
</style>

this also works fine!

and to finally get to my problem:

now i want to have different Colors for "Red" depending on the theme...

so i keep the TextRed style, and add a style to use in Light theme, for testing, i just made a weird color to see if it's working:

<style name="TextRedLight">
    <item name="android:textColor">#ffffff00</item>
</style>

this also work's if i put a switch-statement around the TextView's .setTextAppearance, and select the correct style depending on the selected theme - pseudo code:

switch (currentheme) {
   case Dark: tv.setTextAppearance(... R.style.TextRed); break;
   case Light: tv.setTextAppearance(... R.style.TextRedLight); break;
}

and the challenge / question now is - how can i avoid the switch statement?

When refering to styles in layout.xml files, we can use the ? references to honour the current theme... (instead of "@")

in this case, i need to set the style at run time and honour the theme: so it has to be done by code and not by xml - but setTextAppearance does only accept a id for the style, not a theme-wildcard + id...

and this i where i'm stuck... yes, i could wrap a class around this and keep it to one line in the code and "hide" the theme detection/switch statement in this class, but on the other hand - Android's complex style/theming architecture should have a solution for this problem as well, shouldn't it...?

Using themes and styles seems a bit senseless as long as needing helperclasses or switch/if-statements to get some enhanced stuff done...

Thanks for any hints / ideas!!!

edit 1 - after Tomáš answer: updated style.xml:

<style name="TextNormal" parent="Theme.ARMoDroid">
    <item name="android:textColor">#ffffffff</item>
</style>
 <style name="TextNormal" parent="Theme.ARMoDroid.Light">
    <item name="android:textColor">#ff000000</item>
</style>
<style name="TextRed" parent="Theme.ARMoDroid">
    <item name="android:textColor">#ffff0000</item>
</style>
 <style name="TextRed" parent="Theme.ARMoDroid.Light">
    <item name="android:textColor">#ffffff00</item>
</style>

The TextViews with TextNormal assigned in the layout.xml, or when referencing TextNormal from both themes with:

<item name="android:textAppearance">@style/TextNormal</item>

works as before (with this different approach), also when using red text.

But the initial problem still persists: when i want to change (at runtime) the color of the text to red, using

tv.setTextAppearance(getActivity(),R.style.TextRed)

it always uses the last definition of TextRed - so with above definition, the text will be colored #ffffff00 -> in yellow. when i swap the order of the 2 TextRed definitions, it's using the last definition - #ffff0000 - and the text is red.

So when setting the style at runtime, setTextAppearance is not Theme-Aware - it does not distinguish between the current theme and the 2 defined styles.

we can define one style that has different textcolor-values depending on the theme, and depending on the selected theme it's colored correctly - as long as it's style is assigned in the layout.xml, or the theme set's this style as default.

but when switching the styles dynamically in code at runtime, we still need something like the switch-statement above - detect which theme is selected and depending on the theme, apply a different style to the textview.

what i want to achieve is, that it should pick the correct "TextRed" style depending on the current theme. so no need for hardcoding theme awareness... once a new theme is added the mentioned switch-statement has to be updated as well...

edit 2: my current "solution"

i thought i might also post the current solution, but that's not really a nice solution - though it's working - but it's a custom code for each style and with another theme it's even more code...

The helper function looks like:

     public static int getThemedStyle(int normalStyle) {
     if (prefs==null) { prefs = PreferenceManager.getDefaultSharedPreferences(appContext); }
     int theme = Integer.valueOf(prefs.getString("GUI_Theme","0"));
     if (theme==0) return normalStyle; //dark theme
     else { //return correct  Light-Theme Style
         switch (normalStyle) {
            case R.style.TextNormal: return R.style.TextNormalLight;
            case R.style.TextRed: return R.style.TextRedLight;
            case R.style.TextOrange: return R.style.TextOrangeLight;
            case R.style.TextGreen: return R.style.TextGreenLight;
            case R.style.TextYellow: return R.style.TextYellowLight;
            case R.style.TextBlue: return R.style.TextBlueLight;
            default: return normalStyle;
         } //switch
     } //else theme==0
 } //getThemedStyle

in the fragment i cache the style-reference by calling above function on onCreate:

styleRed = preferences.getThemedStyle(R.style.TextRed);
styleOrange = preferences.getThemedStyle(R.style.TextOrange);
styleGreen = preferences.getThemedStyle(R.style.TextGreen);

and finally assign the style using:

tv.setTextAppearance(getSherlockActivity(), styleRed);

working, but complicated and if that's the way it's itended to be, it's sad... ;)

4

2 回答 2

1

你试过这个吗?

<style name="Theme.MyApp" parent="Theme.Sherlock">
...
</style>

<style name="Theme.MyApp.Light" parent="Theme.Sherlock.Light">
...
</style>

<style name="TextNormal" parent="Theme.MyApp">
    <item name="android:textColor">#ffffffff</item>
</style>

<style name="TextNormal" parent="Theme.MyApp.Light">
    <item name="android:textColor">#ff000000</item>
</style>

layout.xml 中的参考是:

<TextView style="@style/TextNormal" .../>
于 2012-05-22T06:55:27.060 回答
1

回答我自己的问题:

发现这个

所以代替我的 - 工作 - switchstatement,仍然需要一些代码,但它至少不需要对主题 ID 进行硬编码,... -> 用这个片段更新了我的 getThemedStyle

public static int getThemedStyle(int normalStyle) {
 Resources.Theme theme = appContext.getTheme();
 TypedValue styleID = new TypedValue();
 if (theme.resolveAttribute(R.attr.TextAppRed, styleID, true)) return styleID.data;
  else return normalStyle;
} //getThemedStyle
于 2012-05-22T18:07:45.693 回答