How do I change to a Holo Dark/Light themes from within the app?

后端 未结 2 899
悲哀的现实
悲哀的现实 2020-12-16 07:30

I know it\'s possible to have it so if I have a setting I can change between Holo.Light and Holo, however, I cannot seem to find out how. All help is appreciated!

相关标签:
2条回答
  • 2020-12-16 07:51

    I think that you can do it by using the setTheme() method. Just make sure that you call it before you use setContentView, or it won't work.

    For example:

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

    A list of themes can be found here

    0 讨论(0)
  • 2020-12-16 07:52

    As per a comment on the answer posted, if you need to toggle between the default Holo Themes, use this:

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

    To use your own custom defined themes from your Styles.XML file. For example, something like this:

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

    Define this as a Global Variable in your Activity:

    private int mThemeId = -1;
    

    And set your onCreate() method like this:

    @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);
    }
    

    And the code to toggle between the two themes:

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

    Note: The theme has to be set before your call to setContentView()

    0 讨论(0)
提交回复
热议问题