认为我有很多活动,我想要的就是这个:
我在名为test.xml的文件中定义了一个按钮。
我想在我的一个活动中显示该按钮,但只显示该按钮,而不是整个test.xml。
我有一个问题,因为我做了一个像这样的新按钮:
final Button button = (Button) findViewById(R.id.aboutbutton);
但我只是不知道如何只显示那个按钮,而不是孔test.xml。我搜索了android网站,但是从我看到的,它显示了一个洞xml文件,不仅仅是它的一个按钮......所以它不是我需要的......
此代码还有什么问题?错误: 对于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);
}
}
**我像你说的那样修复了代码但是现在一切都变得错了。他们中的大多数都可以修复但是提取......:
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**
答案 0 :(得分:3)
编程方式:
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
Button b=new Button(this);
b.setText("some useful name");//because is empty by default
setContentView(b);
}
xml中的:
布局mulayout.xml中的
代码中的
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.mylayout);
}
这是完整的代码
package com.package.name;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class TestProbaActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button b=new Button(this);
setContentView(b);
}
}
答案 1 :(得分:1)
如果您只想添加按钮,请选择<{1}}或仅在 button.xml 文件中定义一个按钮并设置为it programatically