更改活动主题而不重新创建活动 [英] Changing theme of an activity without recreating activity

查看:73
本文介绍了更改活动主题而不重新创建活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有活动.在按钮上单击,我想更改主题,但是到处都发现只能将主题设置一次,而在setContentView之前也可以设置. 重新启动活动不是我的选择.也许使用attrs可以解决这个问题.但是我不知道如何使用它.请帮忙!

I have an activity . In that on a button click I want to change the theme but everywhere I found that theme can be set only once and that too before setContentView. Restarting an activity is not an option for me. Maybe using attrs has to do something with this problem. But I have no idea how to use it. Please Help!!

提前谢谢!

推荐答案

公共无效setTheme(int resid)

为此上下文设置基本主题.请注意,这应该称为 在Context中实例化任何视图之前(例如 调用setContentView(View)或inflate(int,ViewGroup)).

Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).

因此,在调用setContentView()之前,必须先用setTheme()设置主题,之后才能进行更改.您唯一的选择是重新创建Activity.

So you have to set the theme with setTheme() before calling setContentView(), after that it is impossible to change it. Your only option is to recreate the Activity.

顺便说一句:您说重新创建Activity并不是一种选择,这很不可思议,Activities具有明确的生命周期,并且通过遵守该生命周期,应该可以重新创建Activity或您或Android OS想要的频率.实际上,您应该知道Activity可以随时被Android OS杀死并重新创建,这实际上是Android的正常现象.因此,重新创建Activity并不是一种选择,这似乎表明您的应用程序设计中存在一个更深层次的根源问题,如果不解决的话,它仍然会表现出来.

As an aside: That you say that recreating the Activity is not an option seems weird, Activities have a well defined life cycle and by adhering to this life cycle it should be possible to recreate the Activity how ever often you or the Android OS wants to. In fact you should know that your Activity can be killed and recreated by the Android OS at any moment and this is in fact a normal and expected behaviour of Android. So that recreating the Activity is not an option seems to indicate a much deeper rooted problem in your apps design that will manifest itself anyway if not solved.

这篇关于更改活动主题而不重新创建活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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