4.自定义View

来源:互联网 发布:yunos自动删除软件 编辑:程序博客网 时间:2024/05/21 13:50

1.childrenView
onMeasure设置自己的尺寸
onDraw()绘制要绘制的内容

带自定义属性的View

1.1首先,在资源文件夹values里面新建attrs.xml文件
这里写图片描述

代码:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="textType">        <attr name = "text" format="string"/>        <attr name= "textcolor" format="color"/>    </declare-styleable></resources>

1.2然后在自定义View中绑定属性资源,获取属性值,然后通过获取到的属性值,来对view进行操作:

private void initAttr(AttributeSet attributeSet){    TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.textType);text = typedArray.getString(R.styleable.textType_text);color = typedArray.getColor(R.styleable.textType_textcolor, 0xffffffff);}

1.3 使用要点
1.在xml中静态添加的时候,需要添加
xmlns:sq=”http://schemas.android.com/apk/res/pagername”//pagername为包名
之后,就可以在xml中使用attr中定义的属性了。
如果要动态地操作属性的话,你可以直接在自定义View里面添加公有方法,传值进去,再通过值来操作。(Android自定义View中,顺序为:调用构造函数-》传值方法-》onMeasure-》onLayout-》onDraw)

2.自定义ViewGroup(具体网上一大堆)

顺序(onMeasure –》onLayout)

附要
Android控件的异步刷新方式:
1.handler
2.AsyncTask(doInBackground、)
3.EventBus

2 0
原创粉丝点击