android 动态换肤,Android系统已经完美解决换肤问题,看到网上好多的朋友还在询问如何换肤。我示例一下Android换肤
看效果
点击 Day 后的主题皮肤
点击 Night后,主题皮肤
package cn.hpc.assistant;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
public class MainActivity extends Activity {
private int mThemeId = -1; // 皮肤主题ID,默认-1 不处理
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.getInt("theme", -1) != -1) {// 读取皮肤主题ID,-1 不处理
mThemeId = savedInstanceState.getInt("theme");
this.setTheme(mThemeId); //设置主题皮肤
}
}
setContentView(R.layout.activity_main);
this.findViewById(R.id.id_btn_day).setOnClickListener(mOnClickListener); // day 明亮主题
this.findViewById(R.id.id_btn_night).setOnClickListener(mOnClickListener); //night 黑暗主题
}
View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.id_btn_day:
onTheme(android.R.style.Theme_Light);
break;
case R.id.id_btn_night:
onTheme(android.R.style.Theme_Black);
break;
default:
}
}
};
// 设置主题,并重建
private void onTheme(int iThemeId){
mThemeId = iThemeId;
this.recreate();
}
// 保存主题ID,onCreate 时读取主题
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("theme", mThemeId);
}
}
--------------------------------------------------------------------------
布局文件:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/id_btn_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/id_btn_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day"/>
<Button
android:id="@+id/id_btn_night"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Night" />
</LinearLayout>
转自:http://blog.csdn.net/hpccn/article/details/8432152