相关疑难解决方法(0)

以编程方式切换应用程序范围主题?

我试图获得一个用户选择的主题,感觉我很沮丧.在AndroidManifest.xml工作中定义主题应该是,但(尽我所知)不能根据应用程序首选项更改:

<application 
    android:theme="@style/theme_sunshine" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">
Run Code Online (Sandbox Code Playgroud)

或者,在每个活动中动态设置它也可以:

someChosenTheme = PreferenceManager.getDefaultSharedPreferences(this).getString("themePreference", "theme_twilight");
setTheme(someOtherChosenTheme);
Run Code Online (Sandbox Code Playgroud)

但这看起来很混乱,我宁愿在一个地方设置整个应用程序的主题.我的第一个想法是在我的主要活动启动并在那里执行时抓住应用程序上下文:

getApplicationContext().setTheme(R.style.theme_dummy);
Run Code Online (Sandbox Code Playgroud)

我可以说,应该是诀窍,但实际上它没有做任何事情 - 整个应用程序都有默认的Android风格.以上是否有效,如果是的话,我可能会做一些其他的愚蠢行为吗?

如果重要的话,我在API级别3工作.正确方向的产品非常感谢!

相关问题.

android themes android-theme

38
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×1

android-theme ×1

themes ×1