eclipse .java 不使用 2 或 3 参数视图构造函数;XML 属性不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13797349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:35:14  来源:igfitidea点击:

.java not using the 2- or 3-argument View constructors; XML attributes will not work

androideclipse

提问by xsxy

i'm newbie i have problem creating game

我是新手我在创建游戏时遇到问题

execute process

执行过程

activity_main.xml -> MainActivity.java -> GameLoop.java -> action.xml (error) -> CustomView.java

activity_main.xml -> MainActivity.java -> GameLoop.java -> action.xml (error) -> CustomView.java

Custom view CustomView is not using the 2- or 3-argument View constructors; XML attributes will not work

I don't understand......

我不明白……

回答by Andy McSherry

You need to override the other 2 constructors of View in CustomView:

您需要在 CustomView 中覆盖 View 的其他 2 个构造函数:

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

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

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

private void init(Context context) {
    //do stuff that was in your original constructor...
}

回答by Trevor

You need to implement these constructors also:

您还需要实现这些构造函数:

//Constructor that is called when inflating a view from XML.
View(Context context, AttributeSet attrs)

//Perform inflation from XML and apply a class-specific base style.
View(Context context, AttributeSet attrs, int defStyle)

回答by bianchi

I think it depends how You creating Your custom view and how You gonna use it .
Not all 3 constructors really necessary.
If You create the view with attributes xml file, but won't use defstyle, its enough to call

我认为这取决于您如何创建自定义视图以及您将如何使用它。
并非所有 3 个构造函数都真正必要。
如果您使用属性 xml 文件创建视图,但不会使用 defstyle,则足以调用

public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs);
    //Your code
}

if You not using attributes and defstlye /i saw examples for this/ You happy to call only

如果你不使用属性和 defstlye /我看到了这个例子/你很乐意只打电话

public CustomView(Context context) {
    super(context);
    //Your code
}

and if You want use defstyle and attributes too

如果你也想使用 defstyle 和属性

public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs);
    //Yourcode
}

回答by Tzegenos

In case someone works with Kotlin he/she can do that:

如果有人使用 Kotlin,他/她可以这样做:

class KotlinView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr)

You can find this solution and more details here: https://antonioleiva.com/custom-views-android-kotlin/

您可以在此处找到此解决方案和更多详细信息:https: //antonioleiva.com/custom-views-android-kotlin/