Android程序界面主题切换


这个功能较简单,也就是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换成图片即可!

一起看看效果吧


源码分享:http://download.csdn.net/detail/u010297063/6479203

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值