0

所以我現在有這樣的代碼來改變我的應用程序的主題改變應用程序的主題上運行 -如何使用onCreateView代替的onCreate

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. 

我想知道如何使用這個類合併我的代碼。 (我不必合併它,我可以寫下代碼我只是不知道如何在onCreateView中使用setTheme和其他方法。)

任何幫助,將不勝感激。

+0

你試過'getActivity()。setTheme()'?你必須重新啓動活動 –

+0

還要檢查[這](https://stackoverflow.com/questions/17659413/change-activity-主題從-A-片段) –

回答

1

試試這個onCreateView回調。

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; 
相關問題