天天看點

Android筆記:自定義控件-is not using the 2- or 3-argument Vieconstructors; XML attributes will not work

很久沒有使用過自定義View了,最近在使用的過程中無意間犯了一個小錯誤,記錄下來,在使用自定義控件時出現了以下的錯誤提示:

Android筆記:自定義控件-is not using the 2- or 3-argument Vieconstructors; XML attributes will not work

這段話簡單翻譯一下呢就是自定義View 沒有使用兩個或三個的構造函數,xml檔案不能正常工作

解決方法就是添加兩個參數的構造參數或者是三個構造參數的構造方法。

貼出來代碼簡單看下:

public class RectView extends View {


    public RectView(Context context) {
        super(context);
    }

    public RectView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RectView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}
           

在給自定義View添加構造函數的時候呢,要注意下一定要添加兩個以上。