android自定义控件之extends某控件时,不同构造方法的调用情况分析。

来源:互联网 发布:edm邮件制作软件 编辑:程序博客网 时间:2024/06/05 05:16
public CountDownButton(Context context) { // 方法一super(context);Log.e("CountDownButton", "CountDownButton(Context context)");}public CountDownButton(Context context, AttributeSet attrs) { // 方法二super(context, attrs);Log.e("CountDownButton", "CountDownButton(Context context, AttributeSet attrs)");}public CountDownButton(Context context, AttributeSet attrs, int defStyle) { // 方法三super(context, attrs, defStyle);Log.e("CountDownButton", "CountDownButton(Context context, AttributeSet attrs, int defStyle)");init();}


当我们在代码中调用的时候CountDownButton btn = new CountDownButton(this);

调用的时“方法一”。


当我们如下在布局的xml中,使用的时候,但是并没有调用自定义属性,此时调用的时“方法二”。

<com.test.CountDownButton        android:id="@+id/btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="发送验证码"        android:textSize="14sp" />



当我们如下在布局的xml中,使用的时候且调用自定义属性,此时调用的时“方法三”。

<com.test.CountDownButton        android:id="@+id/btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="发送验证码"
<span style="white-space:pre"></span>app:direction="left"        android:textSize="14sp" />


(1)实际的情况中,我们一般采取方法一调用方法二,方法二调用方法三。因为实际中很多时候都自定义了属性,并且希望不管使用那种方式,

都需要去取得一些默认的属性值并设置给控件。

(2)如果想不是用自定义属性,第三个方法可以不写,这样子就控件默认使用控件自带的属性了。

0 0