我正在尝试在主题之间切换。我查看了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);
}
}
}
main.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>
Styles.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>
答案 0 :(得分:1)
主题应在super.onCreate(savedInstanceState);
来电之前设置。
编辑:
如果你想从savedInstanceState
得到一些东西,你应该先在那里保存一些东西。所以你需要覆盖onSaveInstanceState:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("theme", mThemeId );
}