这个功能较简单,也就是theme的切换问题
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.apptest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:theme就是定义了当前app的主题样式style,这里的定义称之为静态定义,切换主题当然是在主程序里动态切换了!
一 、首先定义一下theme的style:
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="mytheme1" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:textColor">#fff000</item>
<item name="android:background">#00ff00</item>
</style>
<style name="mytheme2" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:textColor">#fff000</item>
<item name="android:background">#fff000</item>
</style>
<style name="mytheme3" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:textColor">#fff000</item>
<item name="android:background">#f0ffff</item>
</style>
这里一共定义了4个主题,第一个是静态的主题,后三个是待切换的主题!
各属性可以百度自行设置,课设置标题栏和背景
二、定义主题配置class
package com.example.apptest;
import android.app.Activity;
import android.content.Intent;
public class themeservice {
private static int stheme;
public final static int theme1=1;
public final static int theme2=2;
public final static int theme3=3;
public static void changetheme(Activity activity,int theme) {
stheme=theme;
activity.finish();
activity.startActivity(new Intent(activity,activity.getClass()));
}
public static void activitySetTheme(Activity activity) {
switch (stheme) {
case 1:
activity.setTheme(R.style.mytheme1);
break;
case 2:
activity.setTheme(R.style.mytheme2);
break;
case 3:
activity.setTheme(R.style.mytheme3);
break;
default:
break;
}
}
}
较简单就不解释了
三、最后也就是在主程序里调用了!
但是有个常识必须要了解的,修改主题必须在setContentView之前执行!为什么?原理较长,百度告诉你!
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
themeservice.activitySetTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()) {
case R.id.button1:
themeservice.changetheme(this, 1);
break;
case R.id.button2:
themeservice.changetheme(this, 2);
break;
case R.id.button3:
themeservice.changetheme(this, 3);
break;
default:
break;
}
}
}
themeservice.activitySetTheme(this);这个代码位置不能放错!
程序很简单,就不用解释了吧?!
这个背景图片照样可以换,style里的background换成图片即可!
一起看看效果吧