2017-10-18 131 views
0
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_create: //run NoteActivity in new note mode 
      startActivity(new Intent(this, NoteActivity.class)); 
      break; 
     case R.id.action_theme: 
      setTheme(R.style.Theme2); 
      setContentView(R.layout.activity_main); 
      Intent i = getIntent(); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(i); 

      // TODO: show settings activity 

      break; 
    } 

    return super.onOptionsItemSelected(item); 
} 

我在我的活動頂部有一個菜單項。我想用它在按下時更改主題。我想在用戶啓動該程序後執行此操作,並最終用於循環使用一系列不同的主題!現在我只想讓它與一個工作。我怎樣才能做到這一點?onCreate()後以編程方式更改主題

我的回答

主要活動

SharedPreferences pref; 
SharedPreferences.Editor editor; 
int check; 
int newcheck; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    pref = getApplicationContext().getSharedPreferences("test", Context.MODE_PRIVATE); 
    check = pref.getInt("x", 0); 
    if(check == 0){ 
     setTheme(R.style.AppTheme); 
    }else{ 
     setTheme(R.style.Theme2); 
    } 

    setContentView(R.layout.activity_main); 
    noteActivity = new NoteActivity(); 
    mListNotes = (ListView) findViewById(R.id.main_listview); 
} 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
      case R.id.action_create: //run NoteActivity in new note mode 
       startActivity(new Intent(this, NoteActivity.class)); 
       break; 
      case R.id.action_theme: 
       pref = getApplicationContext().getSharedPreferences("test", Context.MODE_PRIVATE); 
       editor = pref.edit(); 
       newcheck = pref.getInt("x",0); 
       if(newcheck == 0) { 
        newcheck = 1; 
       }else if(newcheck == 1){ 
        newcheck = 0; 
       } 
       editor.clear();//clears the editor to avoid errors 
       editor.putInt("x",newcheck);//add in new int 
       editor.commit();//commit 

       //restart the activity 
       Intent i = getIntent(); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       finish();//close activity - avoid crash 
       startActivity(i);//start activity 

       //TODO show settings activity 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
+0

那麼你現在面臨的問題是什麼?主題設置不正確? –

+0

當我按下按鈕它不會改變主題。我在onCreate()方法中測試了它,所以主題設置正確。 – pewpew

+0

爲什麼你要再次開始「活動」呢?你應該使用'setTheme'函數,就是這樣。 –

回答

1

至於象我明白你的問題,你可能會考慮採取父Activity思想和設置Fragment有這麼您可以更改Fragment的主題,其中控制更改主題來自您的父母Activity。所以這裏是你如何實現這種行爲。

  • 獲取片段容器在Activity並啓動Fragment在容器中的ActivityonCreate功能。
  • 由於您有一個菜單選項按鈕來決定在Fragment中部署哪個主題,您可能會考慮在單擊菜單選項時通過執行另一個片段事務來重新替換Fragment
  • 你可能會考慮保存所選主題SharedPreferences這樣每次你Fragment啓動,您可以通過在你SharedPreferences閱讀所選主題後設置它在你的FragmentonCreateView功能正確設置的主題。

現在,如果你在想着如何設置一個主題的Fragment然後this post可以幫助你在這方面。爲了您的方便,我從那裏複製代碼。

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

    // create ContextThemeWrapper from the original Activity Context with the custom theme 
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme); 

    // clone the inflater using the ContextThemeWrapper 
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); 

    // inflate the layout using the cloned inflater, not default inflater 
    return localInflater.inflate(R.layout.yourLayout, container, false); 
} 

我希望你已經明白了。請讓我知道是否還有其他需要澄清的內容。

+0

謝謝,但我找到了另一種方法。 +1 – pewpew

+0

我現在唯一的問題是,當我嘗試更改樣式/主題時,當我關閉並重新打開程序時,它會崩潰。 – pewpew

+1

我想通了... – pewpew

相關問題