使用setTheme()动态设置Android应用程序的主题

时间:2015-02-19 03:38:05

标签: android android-fragments

我有一个扩展ActionBarActivity的活动(扩展为能够在我的listFragment中显示动作栏)。我试图在我的活动的OnCreate()方法中使用setTheme()这样设置主题:

setTheme(R.style.AppTheme);
setContentView(R.layout.holder);

但是,我收到此错误消息:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

实际上,我用作setTheme()的参数的样式已经有一个父作为Theme.AppCompat主题:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="background">@color/GhostWhite</item>
</style>

感谢任何答案。

2 个答案:

答案 0 :(得分:0)

创建一个主题对象,并使用setTo(theme)直接应用,如下所示。

Theme theme; 
theme.setTo(other theme which you want to apply);

答案 1 :(得分:0)

弄清楚我的问题:

我最初在清单中的应用程序标记中取出了主题属性。

为了使setTheme()正常工作,似乎需要在清单应用程序标记中设置应用程序范围的主题。然后,您可以在每个活动上使用setTheme()覆盖该应用程序范围的主题。

相关问题