Изменить тему при смене фрагмента - PullRequest
1 голос
/ 17 июня 2019

Мне нужно динамически изменить тему fragment, я пробую этот код, но он не работает на onCreate или onCreateView()

getActivity().setTheme(R.style.ActionMode);

Как установить тему из fragment?

1 Ответ

1 голос
/ 17 июня 2019

В методе onCreateView() вы должны создать ContextThemeWrapper и раздуть из него стиль темы, как показано ниже:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    //create ContextThemeWrapper from the original Activity Context with the custom theme
    Context context = new ContextThemeWrapper(getActivity(), R.style.your_Custom_Theme);
    //clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(context);
    //inflate using the cloned inflater, not the passed in default
    return localInflater.inflate(R.layout.your_layout, container, false);
}
...