您的位置:首页 > 移动开发 > Android开发

Android自定义View以及layout属性全攻略

2014-11-15 22:38 330 查看

Android自定义View以及layout属性全攻略

作者: Android开发网原创 时间: 2010-08-10

对于Android系统的自定义View可能大家都熟悉了,对于自定义View的属性添加,以及Android的Layout的命名空间问题,很多网友还不是很清楚,今天Android123一起再带大家温习一下

CwjView myView=new CwjView(context);

如果用于游戏或整个窗体的界面,我们可能直接在onCreate中setContentView(myView); 当然如果是控件,我们可能会需要从Layout的xml中声明,比如

<cn.com.android123.CwjView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

当然,我们也可以直接从父类声明比如

<View class="cn.com.android123.CwjView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

上面我们仅用了父类View的两个属性,均来自android命名空间,而名称为layout_width或layout_height,我们自定义的控件可能有更多的功能,比如

<cn.com.android123.CwjView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

cwj:age="22"

cwj:university="sjtu"

cwj:city="shanghai"

/>

我们可以看到上面的三个属性,是我们自定义的。作为标准xml规范,可能还包含了类似 xmlns:android="http://schemas.android.com/apk/res/android" 这样的语句,对于定义完整的View,我们的命名空间为cwj,这里可以写为 xmlns:cwj=http://schemas.android.com/apk/res/cn.com.android123.cwjView 或 xmlns:cwj=http://schemas.android.com/apk/res/android 都可以。

对于定义的cwj命名空间和age、university以及city的三个属性我们如何定义呢? 在工程的res/values目录中我们新建一个cwj_attr.xml文件,编码方式为utf-8是一个好习惯,内容如下

<?xml version="1.0" encoding="utf-8" ?>

<resources>

<declare-styleable name="CwjView">

<attr name="age" format="integer" />

<attr name="city" format="string" />

<attr name="university" format="string" />

</declare-styleable>

</resources>

这里我们可能对format不是很熟悉,目前Android系统内置的格式类型有integer比如ProgressBar的进度值,float比如 RatingBar的值可能是3.5颗星,boolean比如ToggleButton的是否勾选,string比如TextView的text属性,当 然除了我们常见的基础类型外,Android的属性还有特殊的比如color是用于颜色属性的,可以识别为#FF0000等类型,当然还有 dimension的尺寸类型,比如23dip,15px,18sp的长度单位,还有一种特殊的为reference,一般用于引用@+id/cwj
@drawable/xxx这样的类型。

当然什么时候用reference呢? 我们就以定义一个颜色为例子,

<attr name="red" format="color|reference" /> 这里我们用了逻辑或的运算符,定义的红色是颜色类型的,同时可以被引用

当然,对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义。在我们自定义View的构造方法(Context context, AttributeSet attrs)的重载类型中可以用

public CwjView(Context context, AttributeSet attrs) {

super(context, attrs);

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.cwj_attr);

mAge = a.getInteger(R.styleable.CwjView_age, 22);

mCity = a.getString(R.styleable.CwjView_city, "shanghai");

mUniversity= a.getString(R.styleable.CwjView_university, "sjtu");

a.recycle(); //Android123提示大家不要忘了回收资源

}

这样类的全局成员变量 mAge、mCity就获取了我们需要的内容,当然根据layout中的数值我们自定义的CwjView需要动态的处理一些数据的情况,可以使用AttributeSet类的getAttributeResourceValue方法获取。

public CwjView(Context context, AttributeSet attrs)

{

super(context, attrs);

resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView", "age", 100);

resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView", "city", "shanghai");

//resID就可以任意使用了

}

总结:

在XML布局文件中使用自定义View主要有以下几个要点:

1、XML布局文件,标签名要包含完整的包名、类名

2、自定义的对象中如果需要用到不属于View类自带的标签名,需要在res/values下新建一个attrs.xml文件,将这些标签名的格式包含进去,写法参照中APIDemo的例子。使用自定义的标签时,前缀为app:,区别于系统自带的android:。

3、新建了attrs.xml文件后,在R文件下才会生成styleable。

4、要让自定义的View的XML标签起作用,在代码里,必须重写构造函数View(Context, AttributeSet)方法。

5、重写的构造方法中,使用obtainStyledAttributes得到XML里的变量的集合,第二个参数名为在attrs.xml里写的 style name。返回值为TypedArray类型。每种类型的变量都有相应的方法得到(不要傻傻地自己去转换= =!!)。得到的值记得判断是否为空值。

6、构造函数最后要使用recycle方法,将得到的TypedArray回收。

7、记得重写onDraw方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: