动态更改活动主题

4

我的应用程序有一个通用的主活动。

主活动从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个回答

4

在调用setContentView(...)super.oncreate()之前,您可以使用setTheme(..),这样就可以正常工作了。


那真的帮助了我:http://javarticles.com/2015/04/android-set-theme-dynamically.html - Tomblarom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接