I am using themes (dynamically) in my android app, like this:
my_layout.xml (extract):
<TextView
android:id="@+id/myItem"
style="?my_item_style" />
attrs.xml (extract):
<attr name="my_item_style" format="reference" />
themes.xml (extract):
<style name="MainTheme.Blue">
<item name="my_item_style">@style/my_item_style_blue</item>
</style>
<style name="MainTheme.Green">
<item name="my_item_style">@style/my_item_style_green<item>
</style>
styles.xml (extract):
<style name="my_item_style_blue">
<item name="android:textColor">@color/my_blue</item>
</style>
<style name="my_item_style_green">
<item name="android:textColor">@color/my_blue</item>
</style>
So, as you can see, I am setting themes dynamically. I am using this class:
public class ThemeUtils {
private static int sTheme;
public final static int THEME_BLUE = 1;
public final static int THEME_GREEN = 2;
public static void changeToTheme(MainActivity activity, int theme) {
sTheme = theme;
activity.startActivity(new Intent(activity, MyActivity.class));
}
public static void onActivityCreateSetTheme(Activity activity)
{
switch (sTheme)
{
default:
case THEME_DEFAULT:
case THEME_BLUE:
activity.setTheme(R.style.MainTheme_Blue);
break;
case THEME_GREEN:
activity.setTheme(R.style.MainTheme_Green);
break;
}
}
}
What I want to know, is there a way how to do this (change theme color) in code? For example, I have following code (extract):
((TextView) findViewById(R.id.myItem)).setTextColor(R.color.blue);
It can be done by some helper method, which would use switch
command for available themes and return correct color for a theme. But I would like to know if there is some better, nicer and faster way.
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…