设置主题为一个片段 [英] Set theme for a Fragment

查看:303
本文介绍了设置主题为一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设定主题的片段。

I'm trying to set the theme for a fragment.

设置主题清单中不工作:

Setting the theme in the manifest does not work:

android:theme="@android:style/Theme.Holo.Light"

这是在看previous博客,它看起来好像我不得不使用ContextThemeWrapper。任何人都可以参考我一个codeD的例子吗?我无法找到任何东西。

From looking at previous blogs, it appears as though I have to use a ContextThemeWrapper. Can anyone refer me to a coded example? I can't find anything.

推荐答案

设置主题清单中通常用于活动。

Setting Theme in manifest is usually used for Activity.

如果你想设置的主题为片段,该片段在onCreateView旁边添加code():

If you want to set Theme for Fragment, add next code in the onCreateView() of the Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

    // inflate the layout using the cloned inflater, not default inflater
    return localInflater.inflate(R.layout.yourLayout, container, false);
}

这篇关于设置主题为一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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