テーマを切り替えようとしています。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>