简单的自定义视图问题

发布于 2024-10-18 12:01:09 字数 1173 浏览 0 评论 0原文

编辑 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2024-10-25 12:01:09

您需要指定更多详细信息 - 您的问题是什么?另外,过去布局 XML 的相关部分。

就其本身而言,该代码看起来基本上是无害的。唯一要记住的是,当您将构造函数调用链接到超类时,也要传递 AttributeSet 。您还必须将此 AttributeSet 传递给子视图的构造函数。

为了。例如:如果您正在创建一个按钮,您会说 new Button(activityContext,attrs);

public ThumbKnightView(Context context,  AttributeSet attrs) {
    super(context,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 this AttributeSet to constructors of your child views.

For. e.g: if you are creating a Button you would say new Button(activityContext,attrs);

public ThumbKnightView(Context context,  AttributeSet attrs) {
    super(context,attrs);
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文