Android应用切换皮肤功能实现(二)
原文地址http://www.apkbus.com/forum.php?mod=viewthread&tid=149034&highlight=%E7%9A%AE%E8%82%A4
上次写完应用切换皮肤功能实现的时候,有网友提了些问题。我觉得上次说的还不够详细吧。现在接着再写详细点。
这次再在布局里增加了三个按钮图片的设置和一个字体颜色的设置。
1.其实上次只是讲到了切换皮肤时,只是替换了图片资源,其实连布局都可以切换。当然布局的切换要是apk类型的皮肤包才行。
首先我们写好3个布局,把三个按钮放在界面的上中下。
1)按钮在上面
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin" >
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="点击界面到设置皮肤界面"
- android:textColor="#000000"
- android:textSize="20sp" >
- </TextView>
- <LinearLayout
- android:layout_alignParentTop="true"
- android:id="@+id/buttonLinear"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/prev"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/prev" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/play"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/play" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/next"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/next" />
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout>
复制代码
2)按钮在中间:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin" >
- <TextView
- android:id="@+id/text"
- android:layout_alignParentBottom="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="点击界面到设置皮肤界面"
- android:textColor="#000000"
- android:textSize="20sp" >
- </TextView>
- <LinearLayout
- android:layout_centerInParent="true"
- android:id="@+id/buttonLinear"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/prev"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/prev" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/play"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/play" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/next"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/next" />
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout>
复制代码
3)按钮在下面:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin" >
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="点击界面到设置皮肤界面"
- android:textColor="#000000"
- android:textSize="20sp" >
- </TextView>
- <LinearLayout
- android:layout_alignParentBottom="true"
- android:id="@+id/buttonLinear"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/prev"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/prev" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/play"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/play" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center" >
- <ImageButton
- android:id="@+id/next"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:scaleType="center"
- android:src="@drawable/next" />
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout>
复制代码
这些布局分别放在不同的apk包里。命名要相同,这里都叫activity_main.xml。
然后在BaseActivity里根据不同的皮肤上下文。就可以创建出不同的布局文件。这样就实现了同一个界面,在不同皮肤里。图片不同,布局也不同。
根据不同皮肤上下文,创建view的代码如下:
- public static View createViewFromResource(Context context,String layoutName,ViewGroup root, boolean attachToRoot) {
- View resultView =null;
- try{
- Context ct =getSkinContext(context);
- int resid = ct.getResources().getIdentifier(layoutName, "layout", ct.getPackageName());
- if(resid != 0){
- resultView= currentInflater.inflate(resid, root, attachToRoot);
- }else{
- resid = context.getResources().getIdentifier(layoutName, "layout",context.getPackageName());
- resultView= defalutInflater.inflate(resid, root, attachToRoot);
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- return resultView;
- }
复制代码
2.另外一个是字体颜色的设置。因为不同的皮肤下,假如字体颜色不跟着皮肤变化的话,看着会非常的别扭。
我们只需要把字体颜色放在一个xml里。不同的皮肤,解析不同的xml得到颜色,就可以实现字体颜色根据皮肤变化了。
这部分代码如下:
- public static int getColorByName(String name){
- int res = -1;
- try {
- String xmlFileName ="skin_color.xml";
- Document doc = getDocumentByFile(xmlFileName);
- if(doc != null){
- String value = getColorByName(doc,name);
- res=Color.parseColor(value);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return res;
- }
- private synchronized static Document getDocumentByFile(String xmlFileName) throws Exception{
- String path = Constant.SKIN_DIR+"skin_color.xml";
- File file = new File(path);
- if(file.exists())
- {
- InputStream inputStream = null;
- try {
- inputStream = new FileInputStream(file);
- return getDocument(inputStream);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return getDocument(MyApplication.getApplication().getAssets().open(xmlFileName));
- }
- private static String getColorByName(Document doc,String name){
- NodeList nodeList= doc.getElementsByTagName(name);
- String res =null;
- if(nodeList != null){
- int len = nodeList.getLength();
- if(len > 0){
- Element el = (Element)nodeList.item(0);
- res = el.getAttribute("value");
- }else{
- }
- }
- return res;
- }
复制代码
下面放上切换皮肤的效果图。无图无真相嘛。
<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应用切换皮肤功能实现(二)的更多相关文章
- Android应用切换皮肤功能实现
原文地址:http://www.eoeandroid.com/thread-318159-1-1.html 现在大多数android应用都支持切换皮肤的功能.比如千千静听,墨迹天气等等.本文介绍两种切 ...
- Android实现换肤功能(二)
前两天写的上章关于换肤的功能获得了很好的反响,今天为大家介绍另一种方式.今天实现的策略也是网友建议的,然后我自己去写了个demo,大家自己评估下相比第一种方式的优势和劣势在哪里. 简单介绍下关于第一种 ...
- android如何切换皮肤
1.先定义attr文件 <?xml version="1.0" encoding="utf-8"?> <resources> <a ...
- [原][osg][osgEarth]EarthManipulator关于oe漫游器的handle部分解读以及修改(仿照谷歌,修改oe漫游器中focal(视角切换)功能 续 二)
bool EarthManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) ...
- Android 实现切换主题皮肤功能(类似于众多app中的 夜间模式,主题包等)
首先来个最简单的一键切换主题功能,就做个白天和晚上的主题好了. 先看我们的styles文件: <resources> <!-- Base application theme. --& ...
- Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能
通过应用程序内置资源实现换肤,典型的应用为QQ空间中换肤的实现. 应用场景为: 应用一般不大,且页面较少,风格相对简单,一般只用实现部分资源或者只用实现背景的更换. 此种换肤方式实现的思路: 1. 把 ...
- jQuery实现无刷新切换主题皮肤功能
主题皮肤切换功能在很多网站和系统中应用,用户可以根据此功能设置自己喜欢的主题颜色风格,增强了用户体验.本文将围绕如何使用jQuery实现点击无刷新切换主题皮肤功能. 查看演示DEMO:https:// ...
- Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验
Android群英传笔记--第十二章:Android5.X 新特性详解,Material Design UI的新体验 第十一章为什么不写,因为我很早之前就已经写过了,有需要的可以去看 Android高 ...
- Android基础——项目的文件结构(二)
Android基础--项目的文件结构(二) AndroidManifest.xml文件分析 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在一个Android项目 ...
随机推荐
- 你所不知道的java编程思想
读thinking in java这本书的时候,有这么一句话“在编译单元的内部,可以有一个公共(public)类,它必须拥有与文件相同的名字” 有以下疑问: 在一个类中说可以有一个public类,那是 ...
- extern C的作用详解
extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...
- Javascript判断空对象
最近在项目开发中判断空对象时,用了“!”运算符,结果程序出现bug,找了好久才找到原因. 其实自己范了一些低级错误,现在把自己经验总结一下: 在JavaScript中,任意JavaScript的值都可 ...
- C#调用进程
Process process = new Process(); process.StartInfo.FileName = "123.exe"; process.StartInfo ...
- [转]C++学习心得
1.把C++当成一门新的语言学习: 2.看<Thinking In C++>: 3.看<The C++ Programming Language>和<Inside The ...
- jar打包命令
jar查看jar命令的使用 1. jar cvf 生成jar包的完整名称 要生成的class文件所在目录以及名称 jar -cvf aa.jar *.* 2.将jar包放到环境变量中,可以不用把cla ...
- Nutch的日志系统
一.Nutch日志实现方式 1.Nutch使用slf4j作为日志接口,使用log4j作为具体实现.关于二者的基础,请参考 http://blog.csdn.net/jediael_lu/article ...
- MySQL 设置远程访问
MySQL远程访问,也就是通过ip访问MySQL服务,MySQL对于安全的要求是非常严格的,需要授权. 1.本地访问 GRANT ALL PRIVILEGES ON *.* TO admin@loca ...
- 利用程序将.jrxml导出为.jasper文件并用PDF显示(2)
直接在Web项目中使用.jasper文件,其实在软件项目开发时,.jrxml文件经常修改,而且.jrxml还要在项目中进行保留备份,所以较为流行的做法是在项目中使用.jrxml来生成.jasper文件 ...
- 数据库语句union的总结
select * from ( (select * from user limit 0,3) union (select * from user limit 10,30) ) tmp where ui ...