【发布时间】:2017-08-12 20:20:39
【问题描述】:
我在我的 Android 应用程序中(动态)使用主题,如下所示:
my_layout.xml(摘录):
<TextView
android:id="@+id/myItem"
style="?my_item_style" />
attrs.xml(摘录):
<attr name="my_item_style" format="reference" />
themes.xml(摘录):
<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(摘录):
<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>
所以,如您所见,我正在动态设置主题。我正在使用这个类:
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;
}
}
}
我想知道的是,有没有办法在代码中做到这一点(更改主题颜色)?例如,我有以下代码(摘录):
((TextView) findViewById(R.id.myItem)).setTextColor(R.color.blue);
这可以通过一些辅助方法来完成,该方法将使用switch 命令获取可用主题并为主题返回正确的颜色。但我想知道是否有更好、更好、更快的方法。
谢谢!
【问题讨论】:
-
你从哪里动态设置主题?你也可以分享那个代码吗?
-
@azizbekian 我添加了 ThemeUtils 类,但我认为它与我的问题无关。
标签: android android-activity colors themes