我需要动态更改 fragment
主题,我尝试了这段代码,但它不适用于 onCreate
或 onCreateView()
getActivity().setTheme(R.style.ActionMode);
如何从 fragment
设置主题?
最佳答案
在 onCreateView()
方法中,您必须创建 ContextThemeWrapper
并从中扩充主题样式,如下所示:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//create ContextThemeWrapper from the original Activity Context with the custom theme
Context context = new ContextThemeWrapper(getActivity(), R.style.your_Custom_Theme);
//clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(context);
//inflate using the cloned inflater, not the passed in default
return localInflater.inflate(R.layout.your_layout, container, false);
}
关于java - 更改 fragment 更改主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56630381/