简单的自定义视图问题
编辑 4:33pm 022111
我忘记包含我的 xml 代码。我真是太傻了
现在我尝试了超级类的属性,但仍然没有帮助...
我尝试搜索所有内容,但无济于事。
我的代码非常非常简单。
以下包括我从创建项目时获得的初始代码中更改的所有内容。
Thumbknightview.java
package com.google.www.Thumbknight;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
public class ThumbKnightView extends View{
public ThumbKnightView(Context context) {
super(context);
}
public ThumbKnightView(Context context, AttributeSet attrs) {
super(context, attrs); // edit 4:33pm 022111
}}
YoAndroid.java
package com.google.www.Thumbknight;
import android.app.Activity;
import android.os.Bundle;
public class YoAndroid extends Activity {
/** Called when the activity is first created. */
//private ThumbKnightView tnv;
float x;
float y;
float lastx;
float lasty;
float maxspeed = 8;
int ipi = -1;
int firstfinger;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
main.xml
我知道这对专家来说可能看起来很愚蠢,但对我来说,它给了我 3 个多小时的搜索和阅读时间。请对我宽容一些。
edit 4:33pm 022111
I forgot to include my xml code. I'm so stupid.
Now I tried attrs to the super class and it still didn't help...
I tried searching for everything but no avail.
My code is very very simple.
The following includes everything I changed from the initial code I get from creating a project.
Thumbknightview.java
package com.google.www.Thumbknight;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
public class ThumbKnightView extends View{
public ThumbKnightView(Context context) {
super(context);
}
public ThumbKnightView(Context context, AttributeSet attrs) {
super(context, attrs); // edit 4:33pm 022111
}}
YoAndroid.java
package com.google.www.Thumbknight;
import android.app.Activity;
import android.os.Bundle;
public class YoAndroid extends Activity {
/** Called when the activity is first created. */
//private ThumbKnightView tnv;
float x;
float y;
float lastx;
float lasty;
float maxspeed = 8;
int ipi = -1;
int firstfinger;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
main.xml
I know it might look stupid for experts but for me, it gave me 3+ hours of searching and reading. Please go easy on me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要指定更多详细信息 - 您的问题是什么?另外,过去布局 XML 的相关部分。
就其本身而言,该代码看起来基本上是无害的。唯一要记住的是,当您将构造函数调用链接到超类时,也要传递
AttributeSet
。您还必须将此 AttributeSet 传递给子视图的构造函数。为了。例如:如果您正在创建一个按钮,您会说
new Button(activityContext,attrs);
You need to specify more details - what is your problem? Also, past relevant section of your layout XML.
The code, as such, looks mostly harmless. The only thing to remember is that when you chain the constructor call to super class, pass
AttributeSet
as well. You will have to also pass thisAttributeSet
to constructors of your child views.For. e.g: if you are creating a Button you would say
new Button(activityContext,attrs);