美文网首页
自定义View中构造方法的调用时机

自定义View中构造方法的调用时机

作者: yangchendev | 来源:发表于2018-03-23 09:59 被阅读0次

我们在继承至View实习自定义View的时候,一般会重写其中的三个构造方法,但你知道这些构造方法在什么情况下会调用吗?

一个参数的构造方法

//这个是在我们使用new关键字来创建View的时候调用
//CTextView ctv =new CTextView(this);
public CTextView(Context context) {

super(context);

}

两个参数的构造方法

//这个会在我们将View写在XML中的时候调用
 public CTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

三个参数的构造方法

//这个会在我们将View写在XML中,并且自定义了style的时候会调用
public CTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

相关文章

网友评论

      本文标题:自定义View中构造方法的调用时机

      本文链接:https://www.haomeiwen.com/subject/flptcftx.html