原文地址http://www.apkbus.com/forum.php?mod=viewthread&tid=149034&highlight=%E7%9A%AE%E8%82%A4

上次写完应用切换皮肤功能实现的时候,有网友提了些问题。我觉得上次说的还不够详细吧。现在接着再写详细点。
这次再在布局里增加了三个按钮图片的设置和一个字体颜色的设置。
1.其实上次只是讲到了切换皮肤时,只是替换了图片资源,其实连布局都可以切换。当然布局的切换要是apk类型的皮肤包才行。
首先我们写好3个布局,把三个按钮放在界面的上中下。
1)按钮在上面

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/main"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin" >
  11. <TextView
  12. android:id="@+id/text"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_centerInParent="true"
  16. android:text="点击界面到设置皮肤界面"
  17. android:textColor="#000000"
  18. android:textSize="20sp" >
  19. </TextView>
  20. <LinearLayout
  21. android:layout_alignParentTop="true"
  22. android:id="@+id/buttonLinear"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content" >
  25. <LinearLayout
  26. android:layout_width="0.0dip"
  27. android:layout_height="fill_parent"
  28. android:layout_weight="1.0"
  29. android:gravity="center" >
  30. <ImageButton
  31. android:id="@+id/prev"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:scaleType="center"
  35. android:src="@drawable/prev" />
  36. </LinearLayout>
  37. <LinearLayout
  38. android:layout_width="0.0dip"
  39. android:layout_height="fill_parent"
  40. android:layout_weight="1.0"
  41. android:gravity="center" >
  42. <ImageButton
  43. android:id="@+id/play"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:scaleType="center"
  47. android:src="@drawable/play" />
  48. </LinearLayout>
  49. <LinearLayout
  50. android:layout_width="0.0dip"
  51. android:layout_height="fill_parent"
  52. android:layout_weight="1.0"
  53. android:gravity="center" >
  54. <ImageButton
  55. android:id="@+id/next"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:scaleType="center"
  59. android:src="@drawable/next" />
  60. </LinearLayout>
  61. </LinearLayout>
  62. </RelativeLayout>

复制代码

2)按钮在中间:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/main"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin" >
  11. <TextView
  12. android:id="@+id/text"
  13. android:layout_alignParentBottom="true"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_centerInParent="true"
  17. android:text="点击界面到设置皮肤界面"
  18. android:textColor="#000000"
  19. android:textSize="20sp" >
  20. </TextView>
  21. <LinearLayout
  22. android:layout_centerInParent="true"
  23. android:id="@+id/buttonLinear"
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content" >
  26. <LinearLayout
  27. android:layout_width="0.0dip"
  28. android:layout_height="fill_parent"
  29. android:layout_weight="1.0"
  30. android:gravity="center" >
  31. <ImageButton
  32. android:id="@+id/prev"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:scaleType="center"
  36. android:src="@drawable/prev" />
  37. </LinearLayout>
  38. <LinearLayout
  39. android:layout_width="0.0dip"
  40. android:layout_height="fill_parent"
  41. android:layout_weight="1.0"
  42. android:gravity="center" >
  43. <ImageButton
  44. android:id="@+id/play"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:scaleType="center"
  48. android:src="@drawable/play" />
  49. </LinearLayout>
  50. <LinearLayout
  51. android:layout_width="0.0dip"
  52. android:layout_height="fill_parent"
  53. android:layout_weight="1.0"
  54. android:gravity="center" >
  55. <ImageButton
  56. android:id="@+id/next"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:scaleType="center"
  60. android:src="@drawable/next" />
  61. </LinearLayout>
  62. </LinearLayout>
  63. </RelativeLayout>

复制代码

3)按钮在下面:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/main"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin" >
  11. <TextView
  12. android:id="@+id/text"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_centerInParent="true"
  16. android:text="点击界面到设置皮肤界面"
  17. android:textColor="#000000"
  18. android:textSize="20sp" >
  19. </TextView>
  20. <LinearLayout
  21. android:layout_alignParentBottom="true"
  22. android:id="@+id/buttonLinear"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content" >
  25. <LinearLayout
  26. android:layout_width="0.0dip"
  27. android:layout_height="fill_parent"
  28. android:layout_weight="1.0"
  29. android:gravity="center" >
  30. <ImageButton
  31. android:id="@+id/prev"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:scaleType="center"
  35. android:src="@drawable/prev" />
  36. </LinearLayout>
  37. <LinearLayout
  38. android:layout_width="0.0dip"
  39. android:layout_height="fill_parent"
  40. android:layout_weight="1.0"
  41. android:gravity="center" >
  42. <ImageButton
  43. android:id="@+id/play"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:scaleType="center"
  47. android:src="@drawable/play" />
  48. </LinearLayout>
  49. <LinearLayout
  50. android:layout_width="0.0dip"
  51. android:layout_height="fill_parent"
  52. android:layout_weight="1.0"
  53. android:gravity="center" >
  54. <ImageButton
  55. android:id="@+id/next"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:scaleType="center"
  59. android:src="@drawable/next" />
  60. </LinearLayout>
  61. </LinearLayout>
  62. </RelativeLayout>

复制代码

这些布局分别放在不同的apk包里。命名要相同,这里都叫activity_main.xml。
然后在BaseActivity里根据不同的皮肤上下文。就可以创建出不同的布局文件。这样就实现了同一个界面,在不同皮肤里。图片不同,布局也不同。
根据不同皮肤上下文,创建view的代码如下:

  1. public static  View createViewFromResource(Context context,String layoutName,ViewGroup root, boolean  attachToRoot) {
  2. View resultView =null;
  3. try{
  4. Context ct =getSkinContext(context);
  5. int resid = ct.getResources().getIdentifier(layoutName, "layout", ct.getPackageName());
  6. if(resid != 0){
  7. resultView= currentInflater.inflate(resid, root, attachToRoot);
  8. }else{
  9. resid = context.getResources().getIdentifier(layoutName, "layout",context.getPackageName());
  10. resultView= defalutInflater.inflate(resid, root, attachToRoot);
  11. }
  12. }catch(Exception e){
  13. e.printStackTrace();
  14. }
  15. return resultView;
  16. }

复制代码

2.另外一个是字体颜色的设置。因为不同的皮肤下,假如字体颜色不跟着皮肤变化的话,看着会非常的别扭。
我们只需要把字体颜色放在一个xml里。不同的皮肤,解析不同的xml得到颜色,就可以实现字体颜色根据皮肤变化了。
这部分代码如下:

  1. public static int getColorByName(String name){
  2. int res = -1;
  3. try {
  4. String xmlFileName ="skin_color.xml";
  5. Document doc  =  getDocumentByFile(xmlFileName);
  6. if(doc != null){
  7. String value = getColorByName(doc,name);
  8. res=Color.parseColor(value);
  9. }
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. return res;
  14. }
  15. private synchronized static Document getDocumentByFile(String xmlFileName) throws Exception{
  16. String path = Constant.SKIN_DIR+"skin_color.xml";
  17. File file = new File(path);
  18. if(file.exists())
  19. {
  20. InputStream inputStream = null;
  21. try {
  22. inputStream = new FileInputStream(file);
  23. return getDocument(inputStream);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. return getDocument(MyApplication.getApplication().getAssets().open(xmlFileName));
  29. }
  30. private static String getColorByName(Document doc,String name){
  31. NodeList nodeList= doc.getElementsByTagName(name);
  32. String res =null;
  33. if(nodeList != null){
  34. int len = nodeList.getLength();
  35. if(len > 0){
  36. Element el = (Element)nodeList.item(0);
  37. res = el.getAttribute("value");
  38. }else{
  39. }
  40. }
  41. return res;
  42. }

复制代码

下面放上切换皮肤的效果图。无图无真相嘛。
<ignore_js_op><ignore_js_op><ignore_js_op><ignore_js_op><ignore_js_op><ignore_js_op>

最后是源码,想要源码,就回复一下吧,反正又不会怀孕的。

本帖隐藏的内容

<ignore_js_op> 2.0.zip (6.5 MB, 下载次数: 117)

Android应用切换皮肤功能实现(二)的更多相关文章

  1. Android应用切换皮肤功能实现

    原文地址:http://www.eoeandroid.com/thread-318159-1-1.html 现在大多数android应用都支持切换皮肤的功能.比如千千静听,墨迹天气等等.本文介绍两种切 ...

  2. Android实现换肤功能(二)

    前两天写的上章关于换肤的功能获得了很好的反响,今天为大家介绍另一种方式.今天实现的策略也是网友建议的,然后我自己去写了个demo,大家自己评估下相比第一种方式的优势和劣势在哪里. 简单介绍下关于第一种 ...

  3. android如何切换皮肤

    1.先定义attr文件 <?xml version="1.0" encoding="utf-8"?> <resources> <a ...

  4. [原][osg][osgEarth]EarthManipulator关于oe漫游器的handle部分解读以及修改(仿照谷歌,修改oe漫游器中focal(视角切换)功能 续 二)

    bool EarthManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) ...

  5. Android 实现切换主题皮肤功能(类似于众多app中的 夜间模式,主题包等)

    首先来个最简单的一键切换主题功能,就做个白天和晚上的主题好了. 先看我们的styles文件: <resources> <!-- Base application theme. --& ...

  6. Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能

    通过应用程序内置资源实现换肤,典型的应用为QQ空间中换肤的实现. 应用场景为: 应用一般不大,且页面较少,风格相对简单,一般只用实现部分资源或者只用实现背景的更换. 此种换肤方式实现的思路: 1. 把 ...

  7. jQuery实现无刷新切换主题皮肤功能

    主题皮肤切换功能在很多网站和系统中应用,用户可以根据此功能设置自己喜欢的主题颜色风格,增强了用户体验.本文将围绕如何使用jQuery实现点击无刷新切换主题皮肤功能. 查看演示DEMO:https:// ...

  8. Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验

    Android群英传笔记--第十二章:Android5.X 新特性详解,Material Design UI的新体验 第十一章为什么不写,因为我很早之前就已经写过了,有需要的可以去看 Android高 ...

  9. Android基础——项目的文件结构(二)

    Android基础--项目的文件结构(二) AndroidManifest.xml文件分析 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在一个Android项目 ...

随机推荐

  1. C#Equal的使用

    代码如下: public partial class FramMain : Form { public FramMain() { InitializeComponent(); } private vo ...

  2. iOS应用性能调优的25个建议和技巧【转】

    转载自:http://blog.jobbole.com/37984/ 首页 最新文章 资讯 程序员 设计 IT技术 创业 在国外 营销 趣文 特别分享 更多 > - Navigation -  ...

  3. (转)sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法

    启动SQL Server 2008 Management Studio 工具菜单----选项----Designers(设计器)----阻止保存要求重新创建表的更改  取消勾选即可.

  4. Java ----------- SQL语句总结(更新中。。。。。。)

    #对数据库的操作 *创建数据库 CREATE DATABASE database_name:database_name为创建的数据库的变量名称. #对表的操作

  5. Android入门3:从Toolbar到Material Design

    在Android5.0(API 21)之后,Toolbar被Google推广,逐渐走入大家视野.具体关于Actionbar和Toolbar的对比就不多啰嗦了,跟着潮流走是没错的.下面先上张简单的效果图 ...

  6. seajs模块压缩问题

    在优化整理项目代码时,想使用seajs来把代码模块化.看了下官方5分钟上手教程,觉得很不错,也没多想就一直开发下去了,也没出什么问题.等一同事说把代码打包个放到设备上去测试一下,发现怎么也跑不起来,郁 ...

  7. POJ2239 二分图最大匹配

    问题:POJ2239 分析: 本题给出每门课程的上课时间,求最大选课数,可以转化为二分图最大匹配问题求解. 设集合A为课程集,集合B为上课时间集,根据输入建立二分图.最大选课书就是该二分图的最大匹配数 ...

  8. 在ubuntu 12.04 中配置java环境(安装jdk, tomcat, maven, eclipse)

    1. 安装jdk 1.7 在ubuntu( /usr/lib/jvm/java-7-openjdk-amd64 )中默认有安装jdk 1.7 如果没有 可下载 : http://www.oracle. ...

  9. debug模式启动provider

    debug 模式启动 1 sts中的配置见图片 2 centos中 ./knowledge-start.sh debug win系统ip ./knowledge-start.sh debug 192. ...

  10. python之pandas模块

    一.pandas模块是基于Numpy模块的,pandas的主要数据结构是Series和DadaFrame,下面引入这样的约定: from pandas import Series,DataFrame ...