0

我正在尝试在主题之间切换。我查看了 HoneyCombGallery 示例代码并找到了在主题之间切换的代码。但它在我的应用程序中不起作用。

代码:

MainActivity.java

public class MainActivity extends Activity {

boolean success = true;
    private int mThemeId = -1;
Button btnViewLib;
Button btnlog;


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

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


    setContentView(R.layout.activity_main);

    btnViewLib = (Button) findViewById(R.id.btnlib);
    btnlog = (Button) findViewById(R.id.btnlog);


    btnViewLib.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Launching All products Activity
            Intent i = new Intent(getApplicationContext(), LibActivity.class);
            startActivity(i);

        }
    });

    btnlog.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Launching All products Activity
            Intent i = new Intent(getApplicationContext(), SomeActivity.class);
            startActivity(i);

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.toggleTheme:
        if (mThemeId == R.style.AppTheme_Dark) {
            mThemeId = R.style.AppTheme_Light;
        } else {
            mThemeId = R.style.AppTheme_Dark;
        }
        this.recreate();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}
}

主要的.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

  <!-- Example of items in the overflow menu -->
  <item android:id="@+id/toggleTheme"
    android:title="@string/theme_toggle"
    android:showAsAction="never" />

 </menu>

样式.xml

<resources>
<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>
</resources>
4

1 回答 1

1

主题应在super.onCreate(savedInstanceState);调用前设置。

编辑:

如果你想从中获取一些东西savedInstanceState,你应该先在里面保存一些东西。所以你需要重写onSaveInstanceState:

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

  savedInstanceState.putInt("theme", mThemeId );

}
于 2013-03-13T12:16:12.903 回答