Values下创建attrs.xml放要定义的属性和值类型
名字+值类型,表面declare-styleable是android系统的,但eclipse不会提示。
<span style="font-size:18px;"><resources>
<declare-styleable name="aview">
<attr name="vleft" format="integer"></attr>
<attr name="vtop" format="integer"></attr>
<attr name="vright" format="integer"></attr>
<attr name="vbottom" format="integer"></attr>
<attr name="color" format="color"></attr>
</declare-styleable>
</resources></span>
布局文件中自定义命名空间把最后的android改成自定义VIew的包名
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aaa="http://schemas.android.com/apk/res/com.bless.screen2d"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.<span style="font-family: Arial, Helvetica, sans-serif;">bless</span><span style="font-family: Arial, Helvetica, sans-serif;">.screen2d.AttrView</span>
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout></span>
构造方法初始化
<span style="font-size:18px;"> public AttrView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.aview);
left = ta.getInteger(0,0);
top = ta.getInteger(1,0);
right = ta.getInteger(2,200);
bottom = ta.getInteger(3,200);
color =ta.getInteger(4, Color.GREEN);
}</span>
这里使用即是自定义属性。可是实现绘图的值由用户传进来。