切换应用程序范围内的主题编程?范围内、应用程序、主题

由网友(不归路)分享简介:我试图获取用户选择的主题,觉得我令人沮丧的接近。定义主题的Andr​​oidManifest.xml 的作品,因为它应该,但(尽我可以告诉)不能改变基于应用程序preferences:I'm attempting to get a user-chosen theme and feel like I'm frustra...

我试图获取用户选择的主题,觉得我令人沮丧的接近。定义主题的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);

作为最好的,我可以告诉,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.

另外,请查阅本教程: 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

阅读全文

相关推荐

最新文章