Android之Merge及自定义属性attrs.xml使用

简介:

   上午看了篇UI优化技巧的文章,《Android Layout Tricks #3: Optimize by merging》是关于<merge/>标签的使用,经常看到别人源码的布局文件使用到<merge/>这个标签,感觉很好奇,今天看了下android文档的表述以及示例有所了解。

   <merge/>的出现是为了优化android布局,减少视图树的层级。当LayoutInflater遇到这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里,从而达到减少层级。这里先看一个简单的布局文件。  

 

 
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" > 
  4.  
  5.     <ImageView 
  6.         android:layout_width="fill_parent" 
  7.         android:layout_height="fill_parent" 
  8.         android:scaleType="center" 
  9.         android:src="@drawable/golden_gate" /> 
  10.  
  11.     <TextView 
  12.         android:layout_width="wrap_content" 
  13.         android:layout_height="wrap_content" 
  14.         android:layout_gravity="center_horizontal|bottom" 
  15.         android:layout_marginBottom="20dip" 
  16.         android:background="#AA000000" 
  17.         android:padding="12dip" 
  18.         android:text="Golden Gate" 
  19.         android:textColor="#ffffffff" /> 
  20.  
  21. </FrameLayout> 

 

上面的根目录是FrameLayout,里面包裹着ImageView以及TextView两个标签。

效果如下图:

 

使用HierarchyViewer 工具来查看该视图的层级效果,我们可以看到蓝色的矩形的就是我们刚刚的FrameLayout

的层数。

将FrameLayout改成merge,代码如下:

 

 
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" > 
  4.  
  5.     <ImageView 
  6.         android:id="@+id/goldenIv" 
  7.         android:layout_width="fill_parent" 
  8.         android:layout_height="fill_parent" 
  9.         android:scaleType="center" 
  10.         android:src="@drawable/golden_gate" /> 
  11.  
  12.     <TextView 
  13.         android:layout_width="wrap_content" 
  14.         android:layout_height="wrap_content" 
  15.         android:layout_gravity="center_horizontal|bottom" 
  16.         android:layout_marginBottom="20dip" 
  17.         android:background="#AA000000" 
  18.         android:padding="12dip" 
  19.         android:text="Golden Gate" 
  20.         android:textColor="#ffffffff" /> 
  21.  
  22. </merge> 

我们看到的效果是这样的,蓝色的是用来包含之前FrameLayout的父标签,

现在直接包裹着ImageView和TextView两个子标签

这个就是merge的简单使用了。

下面再来一个实例,是自定义控件以及自定义属性的使用。

首先我们创建一个布局文件okcalcelbar_button.xml:

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <Button xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:layout_width="wrap_content" 
  4. android:layout_height="wrap_content"/> 


再创建一个okcancelbar.xml的文件:
 

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <merge xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" > 
  5.     <include  
  6.         layout="@layout/okcalcelbar_button" 
  7.         android:id="@+id/okcancelbar_ok" 
  8.         /> 
  9.     <include  
  10.         layout="@layout/okcalcelbar_button" 
  11.         android:id="@+id/okcancelbar_cancel" 
  12.         /> 
  13. </merge> 

创建values下面创建自定义属性的文件attrs.xml

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <declare-styleable name="OkCancelBar">  
  4.         <attr name="okLabel" format="string"/>   
  5.        <attr name="cancelLabel" format="string"/>   
  6.     </declare-styleable>   
  7. </resources> 

 

说明:上面自定义属性文件中

OkCancelBar 就是定义在 <declare-styleable name=" OkCancelBar "></declare-styleable>  里的 名字,获取里面属性用  名字_ 属性  连接起来就可以. TypedArray  通常最后调用  .recycle()  方法,为了保持以后使用该属性一致性!

接下来创建OkCancelBar类继承LinearLayout

 

 
  1. package com.xzw.merge; 
  2.   
  3. import android.content.Context; 
  4. import android.content.res.TypedArray; 
  5. import android.util.AttributeSet; 
  6. import android.view.Gravity; 
  7. import android.view.LayoutInflater; 
  8. import android.widget.Button; 
  9. import android.widget.LinearLayout; 
  10.  
  11. public class OkCancelBar  extends LinearLayout{ 
  12.     public OkCancelBar(Context context,AttributeSet attrs) { 
  13.          super(context,attrs); 
  14.          setOrientation(HORIZONTAL); //横排 
  15.          setGravity(Gravity.CENTER); //居中显示 
  16.          setWeightSum(1.0f); 
  17.           
  18.          LayoutInflater.from(context).inflate(R.layout.okcancelbar, this,true); 
  19.         //TypedArray是一个数组容器   
  20.          TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 00); 
  21.              
  22.             String text = array.getString(R.styleable.OkCancelBar_okLabel);//这里的属性是:名字_属性名   
  23.             if (text == null) text = "Ok"
  24.             ((Button) findViewById(R.id.okcancelbar_ok)).setText(text); 
  25.              
  26.             text = array.getString(R.styleable.OkCancelBar_cancelLabel); 
  27.             if (text == null) text = "Cancel"
  28.             ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text); 
  29.              
  30.             array.recycle(); 
  31.     } 
  32.      

 

 

创建布局文件:

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <merge 
  4.     xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     xmlns:okCancelBar="http://schemas.android.com/apk/res/com.xzw.merge"> 
  6.  
  7.     <ImageView   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.      
  11.         android:scaleType="center" 
  12.         android:src="@drawable/golden_gate" /> 
  13.      
  14.     <com.xzw.merge.OkCancelBar 
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_gravity="bottom" 
  18.         android:paddingTop="8dip" 
  19.         android:gravity="center_horizontal" 
  20.         android:background="#AA000000" 
  21.         okCancelBar:okLabel="Save" 
  22.         okCancelBar:cancelLabel="Don't save" /> 
  23.  
  24. </merge> 

说明:
xmlns:okCancelBar:是我们自定义属性的命名空间前缀。也就是下面

  1.  okCancelBar:okLabel="Save" 
  2.  okCancelBar:cancelLabel="Don't save"

用到的。

”http://schemas.android.com/apk/res/com.xzw.merge" 其中com.xzw.merge 是类文件所在包名。

使用自定义属性必须加上该命名空间。

 

 

看下效果图:

上面就是完整的实例。下面附上代码。

能力有限请大家多多指教。



本文转自xuzw13 51CTO博客,原文链接:http://blog.51cto.com/xuzhiwei/1040484,如需转载请自行联系原作者

 

相关文章
|
10天前
|
Android开发 开发者
安卓应用开发中的自定义视图
【9月更文挑战第37天】在安卓开发的海洋中,自定义视图犹如一座座小岛,等待着勇敢的探索者去发现其独特之处。本文将带领你踏上这段旅程,从浅滩走向深海,逐步揭开自定义视图的神秘面纱。
25 3
|
3天前
|
数据可视化 Android开发 开发者
安卓应用开发中的自定义View组件
【10月更文挑战第5天】在安卓应用开发中,自定义View组件是提升用户交互体验的利器。本篇将深入探讨如何从零开始创建自定义View,包括设计理念、实现步骤以及性能优化技巧,帮助开发者打造流畅且富有创意的用户界面。
13 0
|
2月前
|
供应链 物联网 区块链
未来触手可及:探索新兴技术的趋势与应用安卓开发中的自定义视图:从基础到进阶
【8月更文挑战第30天】随着科技的飞速发展,新兴技术如区块链、物联网和虚拟现实正在重塑我们的世界。本文将深入探讨这些技术的发展趋势和应用场景,带你领略未来的可能性。
|
3天前
|
XML 前端开发 Java
安卓应用开发中的自定义View组件
【10月更文挑战第5天】自定义View是安卓应用开发的一块基石,它为开发者提供了无限的可能。通过掌握其原理和实现方法,可以创造出既美观又实用的用户界面。本文将引导你了解自定义View的创建过程,包括绘制技巧、事件处理以及性能优化等关键步骤。
|
9天前
|
XML 存储 数据格式
|
28天前
|
Android开发 开发者
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
52 10
|
1月前
|
XML 编解码 Android开发
安卓开发中的自定义视图控件
【9月更文挑战第14天】在安卓开发中,自定义视图控件是一种高级技巧,它可以让开发者根据项目需求创建出独特的用户界面元素。本文将通过一个简单示例,引导你了解如何在安卓项目中实现自定义视图控件,包括创建自定义控件类、处理绘制逻辑以及响应用户交互。无论你是初学者还是有经验的开发者,这篇文章都会为你提供有价值的见解和技巧。
35 3
|
1月前
|
前端开发 Android开发 开发者
安卓应用开发中的自定义视图基础
【9月更文挑战第13天】在安卓开发的广阔天地中,自定义视图是一块神奇的画布,它允许开发者将想象力转化为用户界面的创新元素。本文将带你一探究竟,了解如何从零开始构建自定义视图,包括绘图基础、触摸事件处理,以及性能优化的实用技巧。无论你是想提升应用的视觉吸引力,还是追求更流畅的交互体验,这里都有你需要的金钥匙。
|
1月前
|
缓存 搜索推荐 Android开发
安卓应用开发中的自定义View组件实践
【9月更文挑战第10天】在安卓开发领域,自定义View是提升用户体验和实现界面个性化的重要手段。本文将通过一个实际案例,展示如何在安卓项目中创建和使用自定义View组件,包括设计思路、实现步骤以及可能遇到的问题和解决方案。文章不仅提供了代码示例,还深入探讨了自定义View的性能优化技巧,旨在帮助开发者更好地掌握这一技能。
|
2月前
|
XML 搜索推荐 Android开发
安卓开发中的自定义View组件实践
【8月更文挑战第30天】探索Android世界,自定义View是提升应用界面的关键。本文以简洁的语言带你了解如何创建自定义View,从基础到高级技巧,一步步打造个性化的UI组件。