为 Fragment 设置主题 [英] Set theme for a Fragment
本文介绍了为 Fragment 设置主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为片段设置主题.
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"
查看以前的博客,似乎我必须使用 ContextThemeWrapper.任何人都可以向我推荐一个编码示例吗?我找不到任何东西.
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.
推荐答案
在 manifest 中设置 Theme 通常用于 Activity.
Setting Theme in manifest is usually used for Activity.
如果要为 Fragment 设置 Theme,请在 Fragment 的 onCreateView() 中添加以下代码:
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);
}
这篇关于为 Fragment 设置主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文