Android的 - 如何切换主题上运行 [英] Android - How to switch theme on runtime

查看:129
本文介绍了Android的 - 如何切换主题上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我怎样可以从全息切换主题在我上运行的应用程序,以全息光?
我想在设置两个按钮来选择浅色或黑色主题。
怎样才可以应用程序范围并不仅为活动设置?

Could someone tell me how i can switch the the theme from holo to holo light in my application on runtime ? I would like to have two buttons in settings to choose light or black theme. How can it be set applicationwide and not only for the activity ?

我已经尝试过用setTheme()的几件事情,但我不能够改变的主题当我点击一个按钮。

I already tried a few things with setTheme() but i wasn't able to change the theme when i click a button.

这是我设置的活动在这里我想设置主题:

This is my Settings activity where i would like to set the theme:

public class SettingsActivity extends Activity  {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {


setTheme(android.R.style.Theme_Holo_Light);

super.onCreate(savedInstanceState);

setContentView(R.layout.settings);

}

好这个工作,我的主题被设定但我要说我谨pressing一个按钮来更改系统范围。

well this works and my Theme is set but as i am saying i would like to change it systemwide by pressing a button.

谢谢!

推荐答案

正如你所看到的主题是关于设置的onCreate()和前的setContentView()。所以,你应该当你要更改主题再次调用的OnCreate()方法。但的onCreate()函数将在活动的生命周期只有一次调用。

As you can see theme is setting on the onCreate() and before setContentView(). So you should call the oncreate() method again when you want to change the theme. But the onCreate() function will be called only once in the life cycle of an Activity.

有一个简单的方法来做到这一点。我不知道这是最好的方式。

There is a simple way to do this. I am not sure that it is the best way.

假设你想要新的主题应用到 活动1 点击一个按钮。

Suppose you want to apply new theme to Activity 1 on a button click.

onClick事件在

inside the onClick event


  1. 保存主题,应用这样的,它应该(可以用preferences或静态volatile变量)的应用程序重新启动后,即使被保留。

  2. 完成当前活动( 活动1 ),并调用一个新的活动( 活动2

现在 活动2

Now in Activity 2


  1. 呼叫 活动1 并完成当前活动( 活动2 )。

活动1

In Activity 1


  1. 应用保存的主题里面的onCreate

  1. Apply the saved theme inside onCreate.

希望这不是混淆..:)

Hope it is not confusing.. :)

这篇关于Android的 - 如何切换主题上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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