切换应用程序范围内的主题编程? [英] Switching application-wide theme programmatically?

查看:164
本文介绍了切换应用程序范围内的主题编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取用户选择的主题,觉得我令人沮丧的接近。定义主题的Andr​​oidManifest.xml 的作品,因为它应该,但(尽我可以告诉)不能改变基于应用程序preferences:

I'm attempting to get a user-chosen theme and feel like I'm frustratingly close. Defining the theme in AndroidManifest.xml works as it should, but (as best I can tell) can't change based on app preferences:

<application 
    android:theme="@style/theme_sunshine" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">

另外,在每个活动动态地设置它也可以工作:

Alternatively, setting it dynamically in each activity also works:

someChosenTheme = PreferenceManager.getDefaultSharedPreferences(this).getString("themePreference", "theme_twilight");
setTheme(someOtherChosenTheme);

不过,这似乎乱了,我宁愿设置主题在一个地方整个应用程序。我首先想到的是要尽快抢应用程序上下文作为我的主要活动启动并做到有:

But that seems messy, and I'd rather set the theme for the entire app in one place. My first thought was to grab the application context as soon as my main activity launches and do it there:

getApplicationContext().setTheme(R.style.theme_dummy);

作为最好的,我可以告诉,<一个href="http://developer.android.com/reference/android/content/Context.html#setTheme%28int%29">this应该做的伎俩,但实际上它没有做任何事情 - 整个应用程序有默认的A​​ndr​​oid风格。在上述有效的,如果是的话,我可能会做别的事情愚蠢?

As best I can tell, this ought to do the trick, but in fact it's not doing anything - the entire app has the default Android style. Is the above valid, and if so, might I be doing something else dumb?

我的工作在API级别3如果该事项。在正确的方向电棒大大AP preciated!

I'm working in API level 3 if that matters. Prods in the right direction greatly appreciated!

相关问题。

推荐答案

为您的应用程序基础的活动,并覆盖的onCreate设置的主题。源于这个基地活动所有其他活动。

Create a base Activity for your app and override onCreate to set the theme. Derive all your other activities from this base Activity.

另外,请查阅本教程: <一href="http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html">http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

Also check this tutorial: http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

这篇关于切换应用程序范围内的主题编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆