我有一個類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);
}
}
你是什麼意思「我無法添加」?有些東西不允許你這樣做? – eleven
這樣做不應該有任何問題。您需要發佈XML – dymmeh
要在xml中添加自定義視圖,您需要使用它的完整包名稱,例如'' –