android - Activity如何动态改变主题不闪动

查看:162
本文介绍了android - Activity如何动态改变主题不闪动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我现在在给Activity动态换主题时先把Theme id保存在SharedPreferences中,然后用RxBUs发送事件总线让Activity改变Theme,然而这样只有再次打开该Activity主题才会改变;我想通过Activity.recreate()方法立即改变其主题但是有明显闪动,请问有什么好的方法让其不闪动?

解决方案

如果是在Activity中切换,可以考虑按照切换后的主题方案分别设置当前界面所有可见元素的属性;如果是在Fragment中切换,fragment也可以设置theme,像这样:

@Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        final Context contextThemeWrapper = new ContextThemeWrapper(
                getActivity(), isDay ? R.style.day : R.style.night);
        LayoutInflater localInflater = inflater
                .cloneInContext(contextThemeWrapper);
        View v = localInflater.inflate(R.layout.fragment_layout, container,
                false);
        return v;
    }

这篇关于android - Activity如何动态改变主题不闪动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆