如何在Android的XML中插入一个自定义视图 [英] How to inserting a custom view in XML in Android
问题描述
我有一个 A级扩展类查看
。
我有一个 B类扩展的 A级
现在我想补充 B类到我的XML但我无法做到这一点,而我能够添加的 A级到我的XML。
我已经注意到一件事是,这直接延伸查看
所有其他的自定义类是我的XML中可见。
我想知道是否有将延伸另一类从而扩展了类的任何方法查看
进入我的XML?
请注意:我使用的是正确的XML格式,完整的软件包名称
CLASS A
大众A级扩展视图实现Serializable { 大众A(最终上下文的背景下,最终的AttributeSet ATTRS,
最终诠释defStyle,最终SomeEnum的myType){
超(背景下,ATTRS,defStyle);
} 大众A(最终上下文的背景下,最终的AttributeSet ATTRS,
最终SomeEnum的myType){
这(背景下,ATTRS,0,testStateType);
} 大众A(最终上下文的背景下,
最终SomeEnum的myType){
这(背景下,NULL,0,testStateType);
} 大众A(最终上下文的背景下,
最终的AttributeSet ATTRS){
超(背景下,ATTRS);
} @覆盖
保护无效onSizeChanged(最终诠释W,最终诠释小时,最终诠释oldw,
最终诠释oldh){
无效();
} @覆盖
保护无效onMeasure(最终诠释widthMeasureSpec,
最终诠释heightMeasureSpec){
} @覆盖
保护无效的onDraw(帆布油画){ }
}
CLASS B
大众B级扩展了
{
大众B(最终上下文的背景下,
最终的AttributeSet ATTRS,最终诠释defStyle,最终SomeEnum的myType)
{
超(背景下,ATTRS,defStyle,testStateType);
} 大众B(最终上下文的背景下,最终的AttributeSet ATTRS,最终SomeEnum的myType)
{
这(背景下,ATTRS,0,testStateType);
} 大众B(最终上下文的背景下,最终SomeEnum的myType)
{
这(背景下,NULL,0,testStateType);
} @覆盖
保护无效的onDraw(帆布油画)
{
super.onDraw(画布);
}
}
B类缺少缺省参数构造函数。地址:
大众B(最终上下文的背景下,最终的AttributeSet ATTRS){
超(背景下,ATTRS);
}
到类,你将能够从你的其他观点的XML它充气。
在一般情况下,添加自定义的参数到View的构造是不是一个好的做法。使用自定义属性为。
I have a class A that extends class View
.
I have a class B that extends class A.
Now I'm trying to add class B into my xml however I'm unable to do that whereas I'm able to add class A into my xml.
One more thing which I've noticed is that all other custom classes which directly extends View
are visible inside my xml.
I wanna know whether is there any way of adding a class which extends another class which in turn extends View
into my xml?
NOTE : I'm using proper xml format and complete package name.
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);
}
}
class B is missing the constructor with the default arguments. Add:
public B(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
to your class and you will be able to inflate it from an XML with your other views.
In general, adding custom parameters to a View's constructors is not a good practice. Use a custom attribute for that.
这篇关于如何在Android的XML中插入一个自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!