Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...