首页 > 解决方案 > 为应用程序实现不同的材料主题。基于材料设计的猫头鹰应用程序

问题描述

在此示例Material Design Owl 应用程序设计之后,有黄色主题的个性化屏幕、蓝色主题的浏览屏幕和深色主题的学习屏幕,如下所示:

我的问题是我应该为选定的屏幕实施 3 项活动吗?或者我应该为每个片段实现不同的主题?

我更喜欢一个带有喷气背包导航的活动,但是否可以为特定片段设置主题?

标签: androidmaterial-design

解决方案


在 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 中使用经典片段进行。


推荐阅读