自定义控件:
1.新建一个类 Class OwnView extends View{ //必须覆写此构造函数,否则xml中识别不到,运行时会报错. public OwnView(Context ctx,AttributeSet attr){} public void onDraw(Cavas cavas){ //在此处做绘制处理. }
//覆写onMeasured(width,height)中调用setMeasured 设置宽高.
} 2.在布局文件中添加此布局。 <packagePath.OwnView/> 自定义属性: 1.在values目录下attrs.xml中声明styleable <declare-styleable name="OwnViewStyleable"> <attr name="ownargs" format="boolean"> </declare-styleable> 2.在布局文件中的开头位置增加如下内容: <Root xmlns:myapp="http://schemas.android.com/apk/res/packageName"> <packagePath.OwnView myapp:ownargs="true" /> </Root> 3.在OwnView中的构造函数中添加以下代码进行检测: public OwnView(Context ctx,AttributeSet attr) { int[] attrs = R.styleable.OwnViewStyleable; TypedArray a = ctx.obtainStyleAttributes(attr,attrs); boolean ownargsValue = a.getBoolean(R.styleable.OwnViewStyleable_ownargs,false); }