如何使用onCreateview而不是ongreate更改运行时的应用主题



所以我现在有此代码来更改我的应用主题 -

public class SettingsActivity extends AppCompatActivity{
public static int themeCheck = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (themeCheck == 1){
        setTheme(R.style.Dark);
    }
    else{
        setTheme(R.style.Light);
    }
    setContentView(R.layout.fragment_settings);
    }
    // some other code changing other stuff

我正在与我班上的一个人一起研究这个项目,他正在制作实际的骨架代码,并原来他使用了片段,并且没有将活动扩展到AppCompat。因此,我必须将代码与这样的外观合并的课程 -

public class SettingsFrament extends android.support.v4.app.Fragment 
implements MainActivityFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    View rootView = localInflater.inflate(R.layout.fragment_settings, 
    container, false);
    getActivity().setTitle(getString(R.string.settings_fragment_title));
    return rootView;
//This rootView and other bits that that guy gave me is redirecting the app 
//to my activity from the menu bar that he has.

我想知道如何将代码与此类合并。(我不必合并它,我可以将代码写下来,我只是不知道如何使用setTheme和onCreateview中的其他方法。(

任何帮助将不胜感激。

在increateview回调中尝试此操作。

    final Context theme1 = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme1);
    final Context theme2 = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme2);
    LayoutInflater localInflater;
    getActivity().setTitle(getString(R.string.settings_fragment_title));
    if (themeCheck == 1){
         localInflater = inflater.cloneInContext(theme1);
    }
    else{
         localInflater = inflater.cloneInContext(theme2);
    }
    View rootView = localInflater.inflate(R.layout.fragment_settings,
            container, false);
    return rootView;

最新更新