2014-06-17 15 views
-1

我有一個類A擴展類View如何在Android中插入XML定製視圖

我有一個B類延伸A類

現在我想要添加B類到我的XML但我無法做到這一點,而我能夠添加A類到我的XML。

我注意到的另一件事是,所有其他自定義類直接延伸View在我的XML中可見。

我想知道是否有任何方法添加一個類,它擴展了另一個類,然後將View擴展到我的xml中?

注:我使用正確的xml格式和完整的軟件包名稱。

CLASS A

public class A extends View implements Serializable{ 

    public A(final Context context, final AttributeSet attrs, 
      final int defStyle, final SomeEnum myType) { 
     super(context, attrs, defStyle); 
    } 

    public A(final Context context, final AttributeSet attrs, 
      final SomeEnum myType) { 
     this(context, attrs, 0, testStateType); 
    } 

    public A(final Context context, 
      final SomeEnum myType) { 
     this(context, null, 0, testStateType); 
    } 

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

    @Override 
    protected void onSizeChanged(final int w, final int h, final int oldw, 
      final int oldh) { 
     invalidate(); 
    } 

    @Override 
    protected void onMeasure(final int widthMeasureSpec, 
      final int heightMeasureSpec) { 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

    } 
} 

CLASS B

public class B extends A 
{ 
    public B(final Context context, 
      final AttributeSet attrs, final int defStyle, final SomeEnum myType) 
    { 
     super(context, attrs, defStyle, testStateType); 
    } 

    public B(final Context context, final AttributeSet attrs, final SomeEnum myType) 
    { 
     this(context, attrs, 0, testStateType); 
    } 

    public B(final Context context, final SomeEnum myType) 
    { 
     this(context, null, 0, testStateType); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     super.onDraw(canvas); 
    } 
} 
+0

你是什麼意思「我無法添加」?有些東西不允許你這樣做? – eleven

+0

這樣做不應該有任何問題。您需要發佈XML – dymmeh

+0

要在xml中添加自定義視圖,您需要使用它的完整包名稱,例如'' –

回答

1

B類缺少具有默認參數的構造函數。地址:

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

給你的班級,你將能夠通過你的其他視圖從XML擴充它。

通常,向View的構造函數添加自定義參數並不是一個好習慣。爲此使用自定義屬性。

+0

賓果隊友。重點。 +1 – CodeWarrior

1

自定義視圖必須具有以下構造函數:

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

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

public CustomView(Context context, AttributeSet attrs, int defStyle) 
{ 
    super(context, attrs, defStyle); 
} 

看起來你只能有一個接收地重載e SomeEnum參數,但LayoutInflater不知道如何調用這些參數。

+0

非常接近答案隊友,但'SomeEnum'參數不會導致任何問題。雖然這不是一個好習慣,但@ivagarz說。 +1的解釋。 – CodeWarrior

+0

這就是我的意思......你可以有額外的構造函數,但其​​他的都是必需的。你只有那些接收SomeEnum的人。 – matiash

0

爲什麼你得到android膨脹異常錯誤?

這通常是由於xml錯誤引起的。如果您試圖通過非法資源,佈局等來設置視圖的屬性,您的程序將作爲您的大腦崩潰。舉個例子:你不能通過佈局xml文件設置textView背景,你很清楚。但是,當你快速使用日食建議時可能會發生這種情況。看看logcat,你可以看到「二進制XML文件行20」。它告訴你錯誤在哪一行。

造成的:

android.view.InflateException: Binary XML file line #9: Error inflating class

解決方案:

這告訴你定義的類名未找到。仔細檢查課程名稱或檢查構建路徑。 如果其中一個資源具有較高的像素分辨率,則需要很多內存才能導致膨脹異常。

更常見的android InflateExceptions:Android.view.InflateException: Binary XML file line # xx

+0

只需嘗試上面的類,因爲它們是沒有@ivagarz提到的構造函數。在您的xml中使用您的類的完整限定名稱,並嘗試在'getView()'中使其膨脹。您不會收到'ComileTimeException',而會收到'RunTimeException',即'android.view.InflateException'。 – CodeWarrior