关于自定义View的四个构造方法

来源:互联网 发布:adobe网页制作软件 编辑:程序博客网 时间:2024/04/30 23:10

如果用代码来实例化一个自定义的View,会调用第一个构造方法。

View XXX = new View(context);


如果在XML中

<包名+自定义View的名字                android:id="@+id/imageview"                android:layout_width="match_parent"                android:layout_height="180dp"                android:background="@android:color/white"/>

也可以这样:

<View     class="包名+类名"/>

这样就会调用第二个构造函数。

第三个函数系统是不会调用的,要由View显式调用。

例如(第三个构造函数被第二个构造函数调用):

    public OneView(Context context) {        this(context, null);    }    public OneView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public OneView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }







0 0
原创粉丝点击