Android更换皮肤

主工程:

AndroidMainfest.xml按照正常写法即可,无需配置什么东东,当时在设置皮肤的需要这样写上:

Activity.java

 package com.tal.skinmain;

import com.tal.skin.R;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * 
 * @author Tal
 * @time  2011-5-9下午09:32:47
 * @Email dangzhengtao@gmail.com
 * @tag   可以更换皮肤的主工程
 */
public class SkinMain extends Activity {
 
 LinearLayout  linearLayout;
 TextView textview;
 Context ctx;
 Button bt1,bt2,bt3;
 boolean flag = true;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textview = (TextView)findViewById(R.id.id_skin_textview);
        textview.setText("皮肤一");
        linearLayout = (LinearLayout)findViewById(R.id.id_skin_linearlayout);
        
        linearLayout.setBackgroundColor(Color.BLUE);
        
        try {
         //获取皮肤共享的数据包
         ctx = this.createPackageContext("com.tal.skin", CONTEXT_IGNORE_SECURITY);②
        } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
  }
    
  bt1 = (Button)findViewById(R.id.id_skin_bt1);
  bt2 = (Button)findViewById(R.id.id_skin_bt2);
  bt3 = (Button)findViewById(R.id.id_skin_bt3);
  bt1.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    linearLayout.setBackgroundColor(Color.BLUE);
    textview.setText("默认皮肤");
   }});
  bt2.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    linearLayout.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.skin1));  ③
    textview.setText("皮肤一");
   }});
  bt3.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
       linearLayout.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.skin2));④
    textview.setText("皮肤二");
   }});
    }
}

注解:②③④获取皮肤工程的数据,当中有指定皮肤的包名

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/id_skin_linearlayout"
    >
<TextView 
    android:id="@+id/id_skin_textview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
 <Button
   android:id="@+id/id_skin_bt1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="默认"   
   />
    <Button
   android:id="@+id/id_skin_bt2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="皮肤一"   
   />
    <Button
   android:id="@+id/id_skin_bt3"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="皮肤二"   
   />
</LinearLayout>

android 动态换肤,Android系统已经完美解决换肤问题,看到网上好多的朋友还在询问如何换肤。我示例一下Android换肤

看效果

点击 Day 后的主题皮肤

点击 Night后,主题皮肤

[java]  view plain copy

  1. package cn.hpc.assistant;

  2. import android.app.Activity;

  3. import android.content.Context;

  4. import android.graphics.PixelFormat;

  5. import android.graphics.Point;

  6. import android.os.Bundle;

  7. import android.os.Handler;

  8. import android.os.Message;

  9. import android.view.Gravity;

  10. import android.view.View;

  11. import android.view.WindowManager;

  12. import android.view.WindowManager.LayoutParams;

  13. public class MainActivity extends Activity {

  14. private int mThemeId = -1; // 皮肤主题ID,默认-1 不处理

  15. @Override

  16. protected void onCreate(Bundle savedInstanceState) {

  17. super.onCreate(savedInstanceState);

  18. if (savedInstanceState != null) {

  19. if (savedInstanceState.getInt(“theme”, -1) != -1) {// 读取皮肤主题ID,-1 不处理

  20. mThemeId = savedInstanceState.getInt(“theme”);

  21. this.setTheme(mThemeId);  //设置主题皮肤

  22. }

  23. }

  24. setContentView(R.layout.activity_main);

  25. this.findViewById(R.id.id_btn_day).setOnClickListener(mOnClickListener);   // day 明亮主题

  26. this.findViewById(R.id.id_btn_night).setOnClickListener(mOnClickListener);  //night 黑暗主题

  27. }

  28. View.OnClickListener mOnClickListener = new View.OnClickListener() {

  29. @Override

  30. public void onClick(View v) {

  31. // TODO Auto-generated method stub

  32. switch (v.getId()) {

  33. case R.id.id_btn_day:

  34. onTheme(android.R.style.Theme_Light);

  35. break;

  36. case R.id.id_btn_night:

  37. onTheme(android.R.style.Theme_Black);

  38. break;

  39. default:

  40. }

  41. }

  42. };

  43. // 设置主题,并重建

  44. private void onTheme(int iThemeId){

  45. mThemeId = iThemeId;

  46. this.recreate();

  47. }

  48. // 保存主题ID,onCreate 时读取主题

  49. @Override

  50. public void onSaveInstanceState(Bundle outState) {

  51. super.onSaveInstanceState(outState);

  52. outState.putInt(“theme”, mThemeId);

  53. }

  54. }

布局文件:

activity_main.xml

自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

State);

  1. outState.putInt(“theme”, mThemeId);

  2. }

  3. }

布局文件:

activity_main.xml

自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

[外链图片转存中…(img-cOkSSWPp-1723782440174)]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值