【发布时间】:2019-09-08 18:30:27
【问题描述】:
按照这个示例Material Design Owl app design 有黄色主题的个性化屏幕、蓝色主题的浏览屏幕和深色主题的学习屏幕,如下所示:
我的问题是我应该为选定的屏幕实施 3 个活动吗?或者我应该为每个 Fragment 实现不同的主题?
我更喜欢一个带有喷气背包导航的 Activity,但是可以为特定的 Fragment 设置主题吗?
【问题讨论】:
按照这个示例Material Design Owl app design 有黄色主题的个性化屏幕、蓝色主题的浏览屏幕和深色主题的学习屏幕,如下所示:
我的问题是我应该为选定的屏幕实施 3 个活动吗?或者我应该为每个 Fragment 实现不同的主题?
我更喜欢一个带有喷气背包导航的 Activity,但是可以为特定的 Fragment 设置主题吗?
【问题讨论】:
在 manifest 中设置 Theme 通常用于 Activity。
如果要为 Fragment 设置 Theme,在 Fragment 的 onCreateView() 中添加下一段代码:
@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);
}
这将在 Activity 中使用经典 Fragment 进行。
【讨论】: