动态更改活动主题

时间:2011-02-08 16:18:33

标签: android coding-style load themes

我的应用程序有一个通用的主要活动。 主活动从arrays.xml读取其配置,如关注

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="home_buttons_text">
    <item>@string/Title_1</item>
  </string-array>
  <array name="home_buttons_icon">
    <item>@drawable/google</item>
  </array>
  <string-array name="home_buttons_url">
    <item>http://www.google.com</item>
  </string-array>
  <array name="themes">
    <item>@style/my_theme</item>
  </array>
</resources>

通过这种方式添加新图标,我只需在此添加新行。

所以在“主题”中我有主题的名字。我如何加载和应用名称引用的主题?

对于我用过的图标

TypedArray mItemsIcons = resources.obtainTypedArray(R.array.home_buttons_icon);
TypedArray mThemes = resources.obtainTypedArray(R.array.themes);
.
.
.
mItemsIcons.getDrawable(position);
.
.
.

但我不知道如何获得主题。 我已经测试了

int theme = mThemes.getResourceId(position);
setTheme(theme);

但它不起作用。

点击此处查看详细信息:http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray

1 个答案:

答案 0 :(得分:4)

您可以在致电setTheme(..)setContentView(...)之前使用super.oncreate(),它应该可以正常使用

相关问题