在Fragmet中应用Theme

来源:互联网 发布:跟我学plc编程 编辑:程序博客网 时间:2024/06/08 18:34

在一个Fragment中使用了一个ViewPager,每个ViewPager中亦是一个Fragment。形成一个嵌套。

今天碰到一个问题,我要给ViewPager所在的Fragment应用一个主题theme,因为这个Fragment使用了ViewpagerIndicator,我要给其中的TabPageIndicator定制我想要的外观。而看了Demo之后,发现最简单的定制外观的方式就是通过Theme。但是Fragment不是Activity,无法直接在AndroidManifest.xml中写theme,于是在网上google了一下,Fragment应用Theme的方法,最后终于让我找到了,关键代码是这样的:

//使用ContextThemeWrapper通过目标Theme生成一个新的ContextContext ctxWithTheme = new ContextThemeWrapper(getActivity().getApplicationContext(), R.style.Theme_PageIndicatorDefaults);//通过生成的Context创建一个LayoutInflaterLayoutInflater localLayoutInflater = inflater.cloneInContext(ctxWithTheme);//使用生成的LayoutInflater创建ViewrootView = localLayoutInflater.inflate(R.layout.main, container, false);
使用得到的RootVIew就行了~

原创粉丝点击