做Android布局是件很享受的事,这得益于他良好的xml方式。使用xml可以快速有效的为软件定义界面。可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了。那么如何才能做到像官方提供的那些组件一样用xml来定义他的属性呢?现在我们就来讨论一下他的用法。
一、示例
1.1 在res/values文件下定义一个attrs.xml文件
@H_502_39@<?xml version="1.0" encoding="utf-8"?>@H_502_41@ <resources@H_502_41@>@H_502_41@ <declare-styleable@H_502_41@ name@H_502_41@="ToolBar"@H_502_41@>@H_502_41@ <attr@H_502_41@ name@H_502_41@="buttonNum"@H_502_41@ format@H_502_41@="integer"@H_502_41@/>@H_502_41@ <attr@H_502_41@ name@H_502_41@="itemBackground"@H_502_41@ format@H_502_41@="reference|color"@H_502_41@/>@H_502_41@ </declare-styleable@H_502_41@>@H_502_41@ </resources@H_502_41@>@H_502_41@1.2 在布局xml中如下使用该属性
@H_502_39@<?xml version="1.0" encoding="utf-8"?>@H_502_41@ <RelativeLayout@H_502_41@ xmlns:android@H_502_41@="http://schemas.android.com/apk/res/android"@H_502_41@ xmlns:toolbar@H_502_41@="http://schemas.android.com/apk/res/cn.zzm.toolbar"@H_502_41@ android:orientation@H_502_41@="vertical"@H_502_41@ android:layout_width@H_502_41@="fill_parent"@H_502_41@ android:layout_height@H_502_41@="fill_parent"@H_502_41@ >@H_502_41@ <cn.zzm.toolbar.ToolBar@H_502_41@ android:id@H_502_41@="@+id/gridview_toolbar"@H_502_41@ android:layout_width@H_502_41@="fill_parent"@H_502_41@ android:layout_height@H_502_41@="wrap_content"@H_502_41@ android:layout_alignParentBottom@H_502_41@="true"@H_502_41@ android:background@H_502_41@="@drawable/control_bar"@H_502_41@ android:gravity@H_502_41@="center"@H_502_41@ toolbar:buttonNum@H_502_41@="5"@H_502_41@ toolbar:itemBackground@H_502_41@="@drawable/control_bar_item_bg"@H_502_41@/>@H_502_41@ </RelativeLayout@H_502_41@>@H_502_41@1.3 在自定义组件中,可以如下获得xml中定义的值
@H_502_39@TypedArray a = context.obtainStyledAttributes@H_502_41@(attrs,R.styleable@H_502_41@.ToolBar@H_502_41@); @H_502_41@ buttonNum = a.getInt@H_502_41@(R.styleable@H_502_41@.ToolBar@H_502_41@_buttonNum,5@H_502_41@); @H_502_41@ itemBg = a.getResourceId@H_502_41@(R.styleable@H_502_41@.ToolBar@H_502_41@_itemBackground,-1@H_502_41@);@H_502_41@ a.recycle@H_502_41@();@H_502_41@ @H_502_39@TypedArray a = context.obtainStyledAttributes@H_502_41@(attrs,R.styleable@H_502_41@.ToolBar@H_502_41@);@H_502_41@来获得对属性集的引用,然后就可以用“a”的各种方法来获取相应的属性值了。
这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。
当然,在取完值的时候别忘了回收资源哦!
二、declare-styleable的参数解析
2.1 需要用< declare-styleable name=”ToolBar”>包围所有属性
其中name为该属性集的名字,主要用途是标识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用到”R.styleable.ToolBar_buttonNum”,很显然,他在每个属性前面都加了”ToolBar_”。
2.2 支持属性类型
类型 | 示意 | 定义 | 使用 | 备注 |
---|---|---|---|---|
reference | 参考指定Theme中资源ID | < attr name=”label” format=”reference” > | < Button app:label=”@string/app_name” > | |
dimension | 尺寸值 | < attr name=”myWidth” format=”dimension” /> | < Button zkx:myWidth=”100dip”/> | |
float | 浮点型 | < attr name=”fromAlpha” format=”float” /> | < alpha app:fromAlpha=”0.3”/> | |
boolean | 布尔值 | < attr name=”isVisible” format=”boolean” /> | < Button app:isVisible=”false”/> | |
integer | 整型 | < alpha zkx:fromAlpha=”0.3”/> | < animated-rotate app:framesCount=”22”/> | |
string | 字符串 | < attr name=”Name” format=”string” /> | < rotate app:Name=”My name is zhang kun xiang”/> | |
fraction | 百分数 | < attr name=”pivotX” format=”fraction” /> | < rotate app:pivotX=”200%”/> | |
flag | 位或运算 | < attr name=”windowSoftInputMode”>< flag name=”stateUnspecified” value=”1” />< flag name = “adjustNothing” value = “0x30” />< /attr> | < activity android:windowSoftInputMode=”stateUnspecified | adjustNothing”> |
color | 颜色 | < attr name=”textColor” format=”color” /> | < Button zkx:textColor=”#ff0000”/> | |
enum | 枚举 | < attr name=”language”>< enum name=”English” value=”1”/>< /attr> | < Button app:language=”English”/> |
属性定义时可以指定多种类型值:
@H_502_39@<declare@H_502_41@-styleable name = "名称"@H_502_41@> <attr name="background"@H_502_41@ format="reference|color"@H_502_41@ /> </declare@H_502_41@-styleable>使用:
@H_502_39@<ImageView android:background = "@drawable@H_502_41@/图片ID|#00FF00"@H_502_41@/>三、Java中获取
具体属性的获取要拼合定义的name,例如DiyWidget_diy_topDividerShow:
@H_502_39@protected void assignXml(AttributeSet attrs){ if(attrs != null){ TypedArray typedArray = context.obtainStyledAttributes@H_502_41@(attrs,R.styleable@H_502_41@.DiyWidget@H_502_41@);@H_502_41@ if(typedArray != null){ topDividerShow = typedArray.getBoolean@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_topDividerShow,topDividerShow);@H_502_41@ topDividerIndent = typedArray.getBoolean@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_topDividerIndent,topDividerIndent);@H_502_41@ topDividerIndentValue = typedArray.getDimensionPixelSize@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_topDividerIndentValue,topDividerIndentValue);@H_502_41@ bottomDividerShow = typedArray.getBoolean@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_bottomDividerShow,bottomDividerShow);@H_502_41@ bottomDividerIndent = typedArray.getBoolean@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_bottomDividerIndent,bottomDividerIndent);@H_502_41@ bottomDividerIndentValue = typedArray.getDimensionPixelSize@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_bottomDividerIndentValue,bottomDividerIndentValue);@H_502_41@ contentPaddingLeft = typedArray.getDimensionPixelSize@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_contentPaddingleft,contentPaddingLeft);@H_502_41@ contentPaddingRight = typedArray.getDimensionPixelSize@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_contentPaddingRight,contentPaddingRight);@H_502_41@ contentPaddingTop = typedArray.getDimensionPixelSize@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_contentPaddingTop,contentPaddingTop);@H_502_41@ contentPaddingBottom = typedArray.getDimensionPixelSize@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_contentPaddingBottom,contentPaddingBottom);@H_502_41@ initialFocusedWhenTouched = typedArray.getBoolean@H_502_41@(R.styleable@H_502_41@.DiyWidget@H_502_41@_diy_focusedWhenTouched,initialFocusedWhenTouched);@H_502_41@ typedArray.recycle@H_502_41@();@H_502_41@ } } }其他
解析:TypedArray 为什么需要调用recycle()
在 Android 自定义 View 的时候,需要使用 TypedArray 来获取 XML layout 中的属性值,使用完之后,需要调用 recyle() 方法将 TypedArray 回收。
那么问题来了,这个TypedArray是个什么东西?为什么需要回收呢?TypedArray并没有占用IO,线程,它仅仅是一个变量而已,为什么需要 recycle?
为了解开这个谜,首先去找官网的 Documentation,到找 TypedArray 方法,得到下面一个简短的回答:
告诉我们在确定使用完之后调用 recycle() 方法。于是进一步查看该方法的解释,如下:
简单翻译下来,就是说:回收 TypedArray,用于后续调用时可复用之。当调用该方法后,不能再操作该变量。同样是一个简洁的答复,但没有解开我们心中的疑惑,这个TypedArray背后,到底隐藏着怎样的秘密……
我们知道TypedArray 的常规使用方法:
@H_502_39@TypedArray array = context.getTheme@H_502_41@().obtainStyledAttributes@H_502_41@(attrs,R.styleable@H_502_41@.PieChart@H_502_41@,0@H_502_41@,0@H_502_41@);@H_502_41@ try { mShowText = array.getBoolean@H_502_41@(R.styleable@H_502_41@.PieChart@H_502_41@_showText,false);@H_502_41@ mTextPos = array.getInteger@H_502_41@(R.styleable@H_502_41@.PieChart@H_502_41@_labelPosition,0@H_502_41@);@H_502_41@ }finally { array.recycle@H_502_41@();@H_502_41@ }可见,TypedArray不是我们new出来的,而是调用了 obtainStyledAttributes 方法得到的对象,该方法实现如下:
@H_502_39@public@H_502_41@ TypedArray obtainStyledAttributes@H_502_41@(AttributeSet set,int@H_502_41@[] attrs,int@H_502_41@ defStyleAttr,int@H_502_41@ defStyleRes) { final@H_502_41@ int@H_502_41@ len = attrs.length; final@H_502_41@ TypedArray array = TypedArray.obtain(Resources.this@H_502_41@,len); // other code .....@H_502_41@ return@H_502_41@ array; }从上面的代码片段得知,TypedArray也不是它实例化的,而是调用了TypedArray的一个静态方法,得到一个实例,再做一些处理,最后返回这个实例。看到这里,我们似乎知道了什么,,,带着猜测,我们进一步查看该静态方法的内部实现:
@H_502_39@public@H_502_41@ class@H_502_41@ TypedArray@H_502_41@ {@H_502_41@ static@H_502_41@ TypedArray obtain(Resources res,int@H_502_41@ len) { final@H_502_41@ TypedArray attrs = res.mTypedArrayPool.acquire(); if@H_502_41@ (attrs != null@H_502_41@) { attrs.mLength = len; attrs.mRecycled = false@H_502_41@; final@H_502_41@ int@H_502_41@ fullLen = len * AssetManager.STYLE_NUM_ENTRIES; if@H_502_41@ (attrs.mData.length >= fullLen) { return@H_502_41@ attrs; } attrs.mData = new@H_502_41@ int@H_502_41@[fullLen]; attrs.mIndices = new@H_502_41@ int@H_502_41@[1@H_502_41@ + len]; return@H_502_41@ attrs; } return@H_502_41@ new@H_502_41@ TypedArray(res,new@H_502_41@ int@H_502_41@[len*AssetManager.STYLE_NUM_ENTRIES],new@H_502_41@ int@H_502_41@[1@H_502_41@+len],len); } // Other members ......@H_502_41@ }该类没有公共的构造函数,只提供静态方法获取实例,显然是一个典型的单例模式。在代码片段的第 13 行,很清晰的表达了这个 array 是从一个 array pool的池中获取的
结论:
程序在运行时维护了一个 TypedArray的池,程序调用时,会向该池中请求一个实例,用完之后,调用 recycle() 方法来释放该实例,从而使其可被其他模块复用。
那为什么要使用这种模式呢?答案也很简单,TypedArray的使用场景之一,就是上述的自定义View,会随着 Activity的每一次Create而Create,因此,需要系统频繁的创建array,对内存和性能是一个不小的开销,如果不使用池模式,每次都让GC来回收,很可能就会造成OutOfMemory。
这就是使用池+单例模式的原因,这也就是为什么官方文档一再的强调:使用完之后一定 recycle,recycle,recycle