我需要动态更改碎片的主题,我尝试了这段代码,但它在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);
}