分享

在Fragmet中应用Theme

 昵称11482448 2014-05-27

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

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

  1. //使用ContextThemeWrapper通过目标Theme生成一个新的Context  
  2. Context ctxWithTheme = new ContextThemeWrapper(getActivity().getApplicationContext(), R.style.Theme_PageIndicatorDefaults);  
  3. //通过生成的Context创建一个LayoutInflater  
  4. LayoutInflater localLayoutInflater = inflater.cloneInContext(ctxWithTheme);  
  5. //使用生成的LayoutInflater创建View  
  6. rootView = localLayoutInflater.inflate(R.layout.main, container, false);  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多