【发布时间】:2014-05-13 15:44:35
【问题描述】:
我有一个在多个片段之间切换的活动。此活动具有默认样式,但是当我更改为某些特定片段时,我希望它更改样式。我做了一些研究,得到了在片段的 onCreateView() 中运行的代码:
// create ContextThemeWrapper from the original Activity Context with the custom theme
Context context = new ContextThemeWrapper(getActivity(), R.style.GreyTheme);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(context);
// inflate using the cloned inflater, not the passed in default
View rootView = localInflater.inflate(R.layout.my_layout, container, false);
此代码仅在活动重新启动时才有效(例如:如果我旋转设备,它会根据需要更新为新样式)。我认为在不重新创建活动的情况下无法在样式之间切换还是我错了?
【问题讨论】:
-
我发现这适用于在 API23 的片段中膨胀的本地视图,尚未测试其他 API 版本。
-
还有 API17 :)
标签: android android-fragments android-activity android-theme android-styles