2017-10-18 132 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

相关问题