机器人 - 如何不更新主题onCreate事件? [英] android - how to update the theme not in the onCreate event?

查看:297
本文介绍了机器人 - 如何不更新主题onCreate事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我设置这样的主题:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    ThemeSetterActivity.setStyle(Main_MenuActivity.this); //这只是调用context.setTheme();
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main_menu);
}

但我怎么使用更改主题

  ThemeSetterActivity.setStyle(Main_MenuActivity.this);

当它在onresume事件。当我尝试它,它调用函数但主题不会改变。它有事情做与不叫:

  super.onCreate(savedInstanceState);
的setContentView(R.layout.main_menu);


解决方案

要更改应用程序的主题在运行时,您可以使用 setTheme(...)在活动范围内的方法。您必须将主题设定为一个活动,装载该活动的意见了。

有关详细信息和执行,参考链接

更新

和当按照<一个href=\"http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29\"相对=nofollow>文档设置主题,你需要重新启动整个活动。
您可以尝试在 onResume这个code()您的活动,

 意向书I = getIntent();
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ);

In my android app, I set the theme like this:

@Override
public void onCreate(Bundle savedInstanceState){
    ThemeSetterActivity.setStyle(Main_MenuActivity.this); // this just calls context.setTheme();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
}

But how do I change the theme using

ThemeSetterActivity.setStyle(Main_MenuActivity.this);

when it's in the onresume event. When I try it, it does call the function but the theme doesn't change. Does it have something to do with not calling:

super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);

解决方案

To change the theme of your application during run time, you can use the setTheme(...) method within the activity. You must set the theme to an activity, before loading the views of that activity.

For more info and implementation, refer the link:

Updated

And, as per the docs to set theme you need to restart the entire activity. You can try this code in onResume() of your activity,

Intent i = getIntent();
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

这篇关于机器人 - 如何不更新主题onCreate事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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