2012-12-02 2 views

답변

7

setTheme() 메서드를 사용하여 작업 할 수 있다고 생각합니다. setContentView을 사용하기 전에 전화를 걸면 작동하지 않습니다. 테마의 목록은 기본 홀로 테마 사이를 전환해야하는 경우,이를 사용하여 게시 된 대답에 주석 사항에 따라 here

7

을 찾을 수 있습니다

if(userChoice ==1){ 
    setTheme(android.R.style.Theme_Holo_Light); 
else if(userChoice == 2){ 
    setTheme(android.R.style.Theme_Holo); 
} 

: 예를 들어

if (mThemeId == R.style.AppTheme.Dark) { 
     mThemeId = android.R.style.Theme_Holo_Light; 
    } else { 
     mThemeId = android.R.style.Theme_Holo; 
    } 
this.recreate(); 

Styles.XML 파일에서 사용자 정의 된 테마를 사용하려면. 이 같은 예를 들어 뭔가 :

private int mThemeId = -1; 

을 그리고이처럼에서 onCreate() 메소드를 설정합니다 :

<style name="ActionBar" parent="@android:style/Widget.Holo.ActionBar" /> 

<style name="ActionBar.Light" parent="@style/ActionBar"> 
    <item name="android:background">@color/actionbar_background_light</item> 
</style> 

<style name="ActionBar.Dark" parent="@style/ActionBar"> 
    <item name="android:background">@color/actionbar_background_dark</item> 
</style> 

<style name="AppTheme.Light" parent="@android:style/Theme.Holo.Light"> 
    <item name="android:actionBarStyle">@style/ActionBar.Light</item> 
    <item name="android:windowActionBarOverlay">true</item> 
    <item name="listDragShadowBackground">@android:color/background_light</item> 
    <item name="menuIconCamera">@drawable/ic_menu_camera_holo_light</item> 
    <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_light</item> 
    <item name="menuIconShare">@drawable/ic_menu_share_holo_light</item> 
</style> 

<style name="AppTheme.Dark" parent="@android:style/Theme.Holo"> 
    <item name="android:actionBarStyle">@style/ActionBar.Dark</item> 
    <item name="android:windowActionBarOverlay">true</item> 
    <item name="listDragShadowBackground">@android:color/background_dark</item> 
    <item name="menuIconCamera">@drawable/ic_menu_camera_holo_dark</item> 
    <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_dark</item> 
    <item name="menuIconShare">@drawable/ic_menu_share_holo_dark</item> 
</style> 

Activity에서 전역 변수를 정의한다

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if(savedInstanceState != null) { 
     if (savedInstanceState.getInt("theme", -1) != -1) { 
      mThemeId = savedInstanceState.getInt("theme"); 
      this.setTheme(mThemeId); 
     } 
     mTitlesHidden = savedInstanceState.getBoolean("titlesHidden"); 
    } 

    setContentView(R.layout.main); 
} 

그리고 두 테마를 전환하려면 코드 :

if (mThemeId == R.style.AppTheme.Dark) { 
    mThemeId = R.style.AppTheme.Light; 
} else { 
    mThemeId = R.style.AppTheme.Dark; 
} 
this.recreate(); 

참고 : 전화하기 전에 테마를 설정해야합니다.

+0

왜 'android.R.style.DesiredThemeIdHere'를 사용하지 않고 자신 만의 테마를 정의 하시겠습니까? –

+0

@AndreyVoitenkov : 필요하지 않습니다. 나는 단순히 커스텀 테마를 사용하고 런타임에 그것을 변경할 가능성을두고있다. 죄송합니다. 불필요한 경우. – SSL

관련 문제