做Android布局是件很享受的事,这得益于他良好的xml方式。使用xml可以快速有效的为软件定义界面。可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了。那么如何才能做到像官方提供的那些组件一样用xml来定义他的属性呢?现在我们就来讨论一下他的用法。
一、在res/values文件下定义一个attrs.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--
format可使用的值有:
reference:参考某一资源ID。
color:颜色值。
boolean:布尔值。
dimension:尺寸值。
float:浮点值。
integer:整型值。
string:字符串。
fraction:百分数。
enum:枚举值。
flag:位或运算。
-->
<declare-styleable name="DiyViewStyle">
<attr name="ref_col" format="reference|color" />
<attr name="bool" format="boolean" />
<attr name="remain" format="integer|float|fraction|string|enum|dimension" />
<attr name="diyview_fla">
<flag name="val_0" value="0" />
<flag name="val_0x00" value="0x00" />
</attr>
</declare-styleable>
<!-- AndroidManifest.xml可使用布局 -->
<!-- 属性定义时可以指定多种类型值使用|分隔 -->
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:comtu="http://schemas.android.com/apk/res/com.tu.diy.views"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.tu.diy.views.DiyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#5e5e5e"
android:gravity="center"
comtu:ref_col="#fff"
comtu:remain="5518" />
</LinearLayout>
<!-- comtu为xmlns第三行定义的值,可随便修改,但最后面的包名必须与项目包名相同 com.tu.diy.views -->
三、在自定义组件中,可以如下获得xml中定义的值:
package com.tu.diy.views;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class DiyView extends View {
public DiyView(Context context, AttributeSet attrs) {
super(context, attrs);
//获取Xml定义的值: declare-styleable name="DiyViewStyle"
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DiyViewStyle);
// int ref_col = ta.getInt(R.styleable.DiyViewStyle_ref_col, -1);//异常
int col = -1;
try {
col = ta.getColor(R.styleable.DiyViewStyle_ref_col, -1);//注意前DiyViewStyle_ref_col
} catch (Exception e) {
}
int ref = -400;
if (col == -1)
ref = ta.getResourceId(R.styleable.DiyViewStyle_ref_col, -1);
String ref_col = "";
if (ref == -1)
ref_col = ta.getString(R.styleable.DiyViewStyle_ref_col);//自定义的颜色使用getColor获取不到.
String remain = ta.getString(R.styleable.DiyViewStyle_remain);
Log.e("comtu_test", "=====>" + col + "====>" + ref + "=====>" + ref_col + "=====>" + remain);
ta.recycle();//回收
}
public DiyView(Context context) {
super(context);
}
}
就这么简单的三步,即可完成对自定义属性的使用。
*********************************************************************
好了,基本用法已经讲完了,现在来看看一些注意点和知识点吧。
首先来看看attrs.xml文件。
该文件是定义属性名和格式的地方,需要用<declare-styleable name="DiyViewStyle"></declare-styleable>包围所有属性。其中name为该属性集的名字,主要用途是标识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用到"R.styleable.DiyViewStyle_ref_col",很显然,他在每个属性前面都加了"DiyViewStyle_"。
在来看看各种属性都有些什么类型吧:string , integer , dimension , reference , color , enum......
前面几种的声明方式都是一致的,例如:<attr name="buttonNum" format="integer"/>。
只有enum是不同的,用法举例:
<attr name="testEnum">
<enum name="fill_parent" value="-1"/>
<enum name="wrap_content" value="-2"/>
</attr>
如果该属性可同时传两种不同的属性,则可以用“|”分割开即可。
让我们再来看看布局xml中需要注意的事项。
首先得声明一下:xmlns:comtu=http://schemas.android.com/apk/res/com.tu.diy.views
注意,“comtu”可以换成其他的任何名字,后面的url地址必须最后一部分必须用上自定义组件的包名。自定义属性了,在属性名前加上“comtu”即可。
最后来看看java代码中的注意事项。
在自定义组件的构造函数中,用
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DiyViewStyle);
来获得对属性集的引用,然后就可以用“ta”的各种方法来获取相应的属性值了。这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。新版本中的也有直接抛出异常,所以获取资源的时候异常判断齐用好一些。当然,在取完值的时候别忘了回收资源哦!
自定义属性数据类型简介:
一、reference:参考指定Theme中资源ID。
1.定义:
1
2
3
| <declare-styleable name="My">
<attr name="label" format="reference" >
</declare-styleable> |
2.使用:
1
| <Buttonzkx:label="@string/label" > |
二、Color:颜色
1.定义:
1
2
3
| <declare-styleable name="My">
<attr name="textColor" format="color" />
</declare-styleable> |
2.使用:
1
| <Button zkx:textColor="#ff0000"/> |
三、boolean:布尔值
1.定义:
1
2
3
| <declare-styleable name="My">
<attr name="isVisible" format="boolean" />
</declare-styleable> |
2.使用:
1
| <Button zkx:isVisible="false"/> |
四、dimension:尺寸值
1.定义:
1
2
3
| <declare-styleable name="My">
<attr name="myWidth" format="dimension" />
</declare-styleable> |
2.使用:
1
| <Button zkx:myWidth="100dip"/> |
五、float:浮点型
1.定义:
1
2
3
| <declare-styleable name="My">
<attr name="fromAlpha" format="float" />
</declare-styleable> |
2.使用:
1
| <alpha zkx:fromAlpha="0.3"/> |
六、integer:整型
1.定义:
1
2
3
| <declare-styleable name="My">
<attr name="frameDuration" format="integer" />
</declare-styleable> |
2.使用:
1
| <animated-rotate zkx:framesCount="22"/> |
七、string:字符串
1.定义:
1
2
3
| <declare-styleable name="My">
<attr name="Name" format="string" />
</declare-styleable> |
2.使用:
1
| <rotate zkx:pivotX="200%"/> |
八、fraction:百分数
1.定义:
1
2
3
| <declare-styleable name="My">
<attr name="pivotX" format="fraction" />
</declare-styleable> |
2.使用:
1
| <rotate zkx:Name="My name is zhang kun xiang"/> |
九、enum:枚举
1.定义:
1
2
3
4
5
| <declare-styleable name="My">
<attr name="language">
<enum name="English" value="1"/>
</attr>
</declare-styleable> |
2.使用:
1
| <Button zkx:language="English"/> |
十、flag:位或运算
1.定义:
1
2
3
4
5
6
| <declare-styleable name="My">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="1" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable> |
2.使用:
1
| <activity android:windowSoftInputMode="stateUnspecified | adjustNothing"> |
属性定义时可以指定多种类型值:
1
2
3
| <declare-styleable name = "名称">
<attr name="background" format="reference|color" />
</declare-styleable> |
使用:
1
| <ImageView android:background = "@drawable/图片ID|#00FF00"/> |
半原创半转:
http://blog.youkuaiyun.com/congqingbin/article/details/7869730
http://blog.sina.com.cn/s/blog_61f4999d010110o8.html