2017-10-18 2 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에 조각 용기를 얻고 당신의 ActivityonCreate 기능에 해당 컨테이너에 Fragment를 실행합니다.
  • Fragment에 배포 할 테마를 결정하기 위해 메뉴 옵션 버튼이 있으므로 메뉴 옵션을 클릭 할 때 또 다른 조각 트랜잭션을 수행하여 Fragment을 다시 대체하는 것이 좋습니다. 당신이 당신의 SharedPreferences에서 선택한 테마를 읽은 후 당신의 Fragment 당신의 onCreateView 기능으로 설정하여 올바르게 테마를 설정할 수 있습니다 때마다 당신의 Fragment 출시 있도록 SharedPreferences에서 선택한 테마를 저장하는 것이 좋습니다
  • .

이제 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

관련 문제