3

我需要fragment动态更改主题,我尝试了这段代码,但它不起作用onCreateonCreateView()

getActivity().setTheme(R.style.ActionMode);

我如何设置主题fragment

4

1 回答 1

3

在该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);
}
于 2019-06-17T11:44:23.293 回答