主要代码如下
1)设置Theme(res/values/styles)
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- blue -->
<style name="blueTheme">
<item name="android:background">@color/blue</item>
<item name="android:textColor">@color/white</item>
</style>
<!-- black -->
<style name="blackTheme">
<item name="android:background">@color/black</item>
<item name="android:textColor">@color/white</item>
</style>
</resources>
2)调用样式
package com.example.androidtestapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*blue
setTheme(R.style.blueTheme);*/
/*black*/
setTheme(R.style.blackTheme);
setContentView(R.layout.activity_main);
}
@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;
}
}