在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 的属性.如下:

1234567
<?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了.(非常关键哦)
 1 2 3 4 5 6 7 8 91011121314151617
<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).
 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031
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代码:



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

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



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

 1 2 3 4 5 6 7 8 9101112131415161718192021
<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)



或者我的博客: http://www.krislq.com/2013/04/androidclasschange_skin/


更多相关文章

  1. android 自定义progressbar进度条颜色
  2. android switch 控件自定义样式不显示??
  3. Android(Java):自定义控件
  4. android 自定义checkbox
  5. android 自定义线程池ThreadPoolUtils工具类
  6. 【Android】自定义 Tabhost
  7. Android(安卓)自定义ViewPager 实现轮播图
  8. android自定义样式
  9. Android(安卓)ListView使用

随机推荐

  1. Android面经总结
  2. Android(安卓)wakelock机制
  3. Android实现导航菜单左右滑动效果
  4. Android(安卓)Handler机制剖析
  5. 详解 Android(安卓)的 Activity 组件
  6. Android开发者指南Activity and Task Des
  7. Android(安卓)Studio报错adb不是内部命令
  8. Android在任何地方模拟点击屏幕
  9. Android禁止横屏竖屏切换
  10. 【转】IDEA / Android(安卓)studio 使用g