【发布时间】:2011-10-28 17:23:43
【问题描述】:
认为我有很多活动,而我想要的只是这个:
我在名为 test.xml 的文件中定义了一个按钮。
我想在我的一项活动中显示该按钮,但仅显示该按钮,而不是整个 test.xml。
这里有个问题,因为我做了一个这样的新按钮:
final Button button = (Button) findViewById(R.id.aboutbutton);
但我只是不知道如何只显示那个按钮,而不是孔 test.xml。我搜索了android网站,但据我所见,它显示了hole xml文件,不仅仅是1个按钮......所以这不是我需要的......
这段代码还有什么问题?错误:
未定义 View Gameview.java 类型的 onCreate(Bundle) 方法
Gameview 类型的 onCreate(Bundle) 方法必须覆盖超类方法 Gameview.java
public class Gameview extends View{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutbutton);
}
private final Game game;
public Gameview(Context context) {
super(context);
this.game = (Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// Draw the background...
Paint background = new Paint();
background.setColor(getResources().getColor(
R.color.background));
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
final Button button = (Button) findViewById(R.id.aboutbutton);
}
}
**我像你说的那样修复了代码,但现在一切都出错了。它们中的大多数都可以修复,但是 ondraw ...:
aboutbutton cannot be resolved or is not a field Gameview.java
The constructor Activity(Context) is undefined Gameview.java
The method getHeight() is undefined for the type Gameview
The method getWidth() is undefined for the type Gameview
The method onDraw(Canvas) of type Gameview must override a superclass method Gameview.java
The method requestFocus() is undefined for the type Gameview Game.java m
The method setContentView(int) in the type Activity is not applicable for the arguments (Gameview) Game.java
The method setFocusable(boolean) is undefined for the type Gameview
The method setFocusableInTouchMode(boolean) is undefined for the type Gameview Gameview.java**
【问题讨论】:
-
只是学习Android-View和Android-Activity的基础知识以及如何使用它们?而不是直接应用它。