我有几个片段之间切换的活动。本次活动有一个默认的风格,但是当我更改为某个特定的片段我希望它改变了风格。我做了一些研究,我得到这个$ C $的片段c表示我在onCreateView运行():
I have an activity that switches between several fragments. This activity has a default style, but when i change to some particular fragments i want it to change the style. I've done some research and i got this code that i run in the onCreateView() of a fragment:
// create ContextThemeWrapper from the original Activity Context with the custom theme
Context context = new ContextThemeWrapper(getActivity(), R.style.GreyTheme);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(context);
// inflate using the cloned inflater, not the passed in default
View rootView = localInflater.inflate(R.layout.my_layout, container, false);
如果该活动将重新启动该code只适用(例如:如果我旋转它更新为新样式的设备,因为我想要的)。我认为这是不可能不用其他重建活动方式之间切换还是我错了?
This code only works if the activity is restarted (eg: if i rotate the device it updates to the new style as i want). I think that it is not possible to switch between styles witout recreating an activity or am i wrong?
推荐答案
从技术上讲:无。
如果该活动已创建,即不能改变当前的主题。
You can not change the current theme if the Activity has been created.
如果该活动将重新启动该code只适用(例如:如果我旋转 设备它更新到新的风格,因为我想要的)。我认为这是不 可以不用其他重建活动方式之间切换还是我 我错了吗?
This code only works if the activity is restarted (eg: if i rotate the device it updates to the new style as i want). I think that it is not possible to switch between styles witout recreating an activity or am i wrong?
由于旋转包含了活动的娱乐,这是为什么工作的原因。
As rotating includes a recreation of the activity, this is the reason on why is "working".
但是......有一个应用程序被称为掌上(也preSS和Firefox如果我没记错的话),这是否在一个聪明的办法。
But… there is one application called Pocket (also Press and Firefox if I remember correctly) that does this in a clever way.
如何办呢?
基本上诀窍就在于这个公式:
Basically the trick lies in this formula:
Base Color1 + Middle Color = Theme Color 1
Base Color2 + Middle Color = Theme Color 2
请记住,中间颜色是一样的。对于基本颜色,你必须把它放到窗口牵着你的应用程序实例,是这样的:
Keep in mind that Middle Color is the same. For the base color you have to put it to the Window holding your app instance, something like this:
getWindow().setBackgroundDrawable(new ColorDrawable(isLight ? Color.WHITE : Color.BLACK));
因此,当结合了中间色,提供了两个不同的主题。
Therefore when is combined with an intermediate color, gives you two different themes.
在这里,你可以看到你是如何做到这一点(它解释的概念非常好):
Here you can see how you can do it (it explains the concept very well):
HTTP://sriramramani.word$p $ pss.com / 2012/12/06 /运行时主题改变/
修改1:
增加了更多的解释,该链接的帖子
Added more explanations to the link posts
相关推荐
最新文章