【问题标题】:How to show only the button如何只显示按钮
【发布时间】: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的基础知识以及如何使用它们?而不是直接应用它。

标签: android xml button


【解决方案1】:

以编程方式:

    @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);
          }
     }

【讨论】:

  • 构造函数 Button(Gameview) 未定义 为什么会出现?我导入了 widget.Button ?!
  • 这是我的导入列表:import android.app.Activity;导入android.os.Bundle;导入android.view.View;导入android.view.ViewGroup;导入 android.widget.ArrayAdapter;导入android.widget.Button;导入 android.widget.TextView;
  • 我在上面发布了我的代码...请告诉我出了什么问题...我导入了您拥有的所有内容。
  • 看我的编辑,你有很多问题,第一个是你必须扩展活动而不是查看
  • 你的问题在别处,为了向整个活动显示一个按钮,上面的代码工作正常,我试过了
【解决方案2】:

如果您只想添加按钮,则要么创建it programatically,要么只在button.xml 文件中定义一个按钮并设置为setContentView(R.layout.button);

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 2013-08-23
    • 1970-01-01
    • 2013-08-18
    • 2018-05-31
    • 2021-09-18
    • 1970-01-01
    • 2021-09-14
    • 2018-02-13
    相关资源
    最近更新 更多