一个视图从创建到显示过程中的主要方法
1,构造方法实例化类
2,测量measure(int,int)-->onMeasure();
如果当前View是一个ViewGroup,还有义务测量孩子
孩子有建议权
3,指定位置layout()-->onLayout();
指定控件的位置,一般View不用写这个方法,ViewGroup的时候才需要,一般View不需要重写该方法
4,绘制视图-->draw()-->onDraw(canvas)
根据上面两个方法参数,进入绘制
显示在main布局里面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CustomButton">
<com.example.custombutton.MyToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
代码如下,其余不接受:
public class MyToggleButton extends View implements View.OnClickListener {