在long long ago ,写过两篇文章来实现换肤,在论坛里面不敢说人人皆知,但是人气还是到位了的.如果你还不知道,那赶快再去看看吧:
[Android实例] 【Kris专题】android 换肤 http://www.eoeandroid.com/thread-102060-1-1.html
[Android实例] 【kris专题】android 换肤(续) http://www.eoeandroid.com/thread-102536-1-1.html
另外一些在本文中会使用到的些知识:
【Kris专题】android Style 小结 http://www.eoeandroid.com/thread-99671-1-1.html
[Android实例] 【Kris专题】android:shape的使用 http://www.eoeandroid.com/thread-105450-1-1.html


然后今天我将给大家带来更神奇也更方便的一种方法来完成换肤.至于大家在项目中使用哪种方法就看大家的实际的项目需求了.
这种换肤的主要思路是通过 Style来完成,如果大家对 Style的应用还不是很清楚,或者说是甚至没听说过,那你就危险了,至少足以证明你在Android界里面有多菜了.如果应用不是很熟悉,或者说他的原理不是很清楚的,自己可以多查阅一些资料,在这里就不作更仔细的介绍了.
我们还是通过Demo 来一步步守成整个换肤的过程吧..
1.当然是建议一个Android projects. 项目名什么的就随便你自己取了.我推荐大家在写自己的一些 demo的时候,指导编译 sdk可以设置到最高,别老是在2.x 徘徊了,有意思吗?
2.项目建议好之后,就准备好各种我们图片资源文件吧(你可以ps一些背景图,也可以通过style中的shape来制作一些资源.或者说在color.xml中定义好一些颜色值,当然最方便的还是下载我的demo啦,里面啥都有了,呵呵 )

3.在res/values目录下建议一个attrs.xml.里面用来定义一些style 的属性.如下:


<?xml version="1.0" encoding="utf-8"?><resources><attr name="button" format="reference"/><attr name="background" format="reference"/><attr name="textColor" format="reference"/></resources>

代码解释:
在上面的代码中,我们定义了三个属性(button,backgroud,textColor),这三个属性呢会在Style中用到,并且指出它的格式是引用形的.
比如说 button,它的值到时候我们会在style里面去设置,并且指向具体的某个资源文件.

4.然后到我们的res/values/style.xml了.(非常关键哦)

<resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="AppTheme_Default" parent="@android:style/Theme.Holo.Light.DarkActionBar"><item name="button">@drawable/btn_grey</item><item name="background">@drawable/backgroud2</item><item name="textColor">@color/red</item></style><style name="AppTheme_Another" parent="AppTheme_Default"><item name="button">@drawable/btn_blue</item><item name="background">@drawable/backgroud1</item><item name="android:actionBarStyle">@style/ActionBarStyle</item></style><style name="ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"><item name="android:background">#33b5e5</item></style></resources>

代码解释:
在这里我们定义了三个style (AppTheme_Default,AppTheme_Another,ActionBarStyle),其中AppTheme_Default,AppTheme_Another表示我们的两套主题.ActionBarStyle为我们actionBar的样式.
AppTheme_Default为默认的主题,它是继承至@android:style/Theme.Holo.Light.DarkActionBar.当然你也可以继承Android中任意的已经有的主题 或者是style , 你也可以选择不继承任何已经有的样式 .
AppTheme_Another为我们第二套主题 ,它是继承至AppTheme_Default的.在这里我们可以覆写部分AppTheme_Default里面的值,也可以覆写全部AppTheme_Default里面的值,这个看你具体的主题,也可以扩展其它一些新的值.
比如说在AppTheme_Another中,没有覆写textColor的值 ,也就是在这两种主题中,我的文字的颜色值都是使用AppTheme_Default主题 中的值,都为红色.但是又新增了一个android:actionBarStyle的样式来改变actionBar 的样式.

5.接下来呢,我们再实现一个基础类,BaseActivity来代替所有的activity完成主题的切换(当然要求程序里面所有的activity 都得继承至 BaseActivity).



public class BaseActivity extends Activity {public int mTheme = R.style.AppTheme_Default;@Overrideprotected void onCreate(Bundle savedInstanceState) {if (savedInstanceState == null) {mTheme = PreferenceHelper.getTheme(this);} else {mTheme = savedInstanceState.getInt("theme");}setTheme(mTheme);super.onCreate(savedInstanceState);}@Overrideprotected void onResume() {super.onResume();if (mTheme != PreferenceHelper.getTheme(this)) {reload();}}protected void reload() {Intent intent = getIntent();overridePendingTransition(0, 0);intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);finish();overridePendingTransition(0, 0);startActivity(intent);}}

代码解释:
onCreate中,我们从preference中去获取到主题,并且设置给 activity.
在onresume中再去检查主题 是否已经改变? 如果已经改变了,就重新加载activity ,否则没有动作.
reload就是finish当前 activity ,再启动当前activity啦.
6.万事俱备了,现在的问题是我们在atrrs中定义的button , background怎么使用呢?
a).java代码:

int[] attrs = new int[]{R.attr.button};TypedArray typedArray = context.obtainStyledAttributes(attrs);

具体的使用大家自行去测试了



b).直接在layout文件里面使用.比如我们 demo中的两个布局文件 .在这里我们例出一个来讲解.
second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="?attr/background" ><TextView android:id="@+id/textview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="?attr/textColor"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:text="@string/msg_second" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="?attr/button"android:text="@string/btn_text" /></RelativeLayout>


代码解释:
android:background=&quot;?attr/background&quot;在这里使用了我们在attrs.xml中的background属性.
android:textColor=&quot;?attr/textColor&quot; 使用了我们在attrs.xml中定义的textColor
android:background=&quot;?attr/button&quot; 在这里使用了我们在attrs.xml中定义的button属性
我们再来走一个这个调用的流程.
在生成布局文件中RelativeLayout的时候,系统找到attrs.xml中定义的backround,然后再去当前设置的theme(我们假设是AppTheme_Default)中找到background指向的资源@drawable/backgroud2然后加载到内存赋值给RelativeLayout的android:background.如果当前主题 是AppTheme_Another,就会导入@drawable/backgroud1给RelativeLayout的android:background
好的.基本就讲到这里了.大家可以多看看 demo .多自己摸过下.
具体的 Demo下载地址: [http://www.eoeandroid.com/thread-264902-1-1.html](http://www.eoeandroid.com/thread-264902-1-1.html)

更多相关文章

  1. Android(安卓)UI学习 - ListView
  2. 2011.09.26(2)——— android sample之Notepad(getSelectedItemId
  3. android 通信机制 socket
  4. Base64编解码Android和ios的例子,补充JNI中的例子
  5. Android(安卓)中的各种控件
  6. Android(安卓)导入项目时遇到的JNI和NDK问题
  7. Android(安卓)最火框架XUtils之注解机制详解
  8. android studio里面的svn基本使用
  9. Base64编解码Android和ios的例子,补充JNI中的例子

随机推荐

  1. Android(安卓)仿微信点赞和评论弹出框
  2. Android(安卓)6.0 悬浮窗默认关闭解决方
  3. 在小米手机上,修改APP的launcher应用图标
  4. Android内存泄漏原因及解决办法
  5. 【Android】缩略图Thumbnails
  6. [图解]Android(安卓)View的事件分发机制
  7. Android(安卓)APP存活检测方式
  8. Android点9图的运用
  9. Android混淆从入门到精通
  10. Android白盒测试情况