【问题标题】:Implementing different material themes to app. Based on material design owl app为应用程序实现不同的材料主题。基于材料设计的猫头鹰应用程序
【发布时间】:2019-09-08 18:30:27
【问题描述】:

按照这个示例Material Design Owl app design 有黄色主题的个性化屏幕、蓝色主题的浏览屏幕和深色主题的学习屏幕,如下所示:

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

我更喜欢一个带有喷气背包导航的 Activity,但是可以为特定的 Fragment 设置主题吗?

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    在 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 进行。

    【讨论】:

    • 但它只会将主题应用于 Fragment,而不是 Activity 和操作栏
    • 可以在fragment中设置action bar的默认颜色。
    猜你喜欢
    • 2017-10-21
    • 1970-01-01
    • 2019-07-29
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    相关资源
    最近更新 更多