search

Home  >  Q&A  >  body text

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

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

ringa_leeringa_lee2767 days ago528

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 17:42:30

    If you are switching in an Activity, you can consider setting the attributes of all visible elements of the current interface according to the switched theme plan; if you are switching in a Fragment, the fragment can also set the theme, like this:

    @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;
        }

    reply
    0
  • PHPz

    PHPz2017-04-17 17:42:30

    Bilibili has open sourced a solution on github, you can take a look

    reply
    0
  • Cancelreply