Как показать только кнопку - PullRequest
1 голос
/ 28 октября 2011

Подумайте, что у меня много занятий, и все, что мне нужно, это:

У меня есть кнопка, определенная в файле с именем test.xml.

Я хочу показать эту кнопку водно из моих занятий, НО только эта кнопка, а не весь test.xml.

Здесь у меня проблема, потому что я сделал новую кнопку, подобную этой:

final Button button = (Button) findViewById(R.id.aboutbutton);

Но я простоне знаю, как отобразить только эту кнопку, а не дыру test.xml.Я искал на сайте Android, но из того, что я видел, он показывает XML-файл дыры, а не только 1 кнопку от него ... так что это не то, что мне нужно ...

И чтоне так с этим кодом?ошибки: метод onCreate (Bundle) не определен для типа View Gameview.java
Метод onCreate (Bundle) типа Gameview должен переопределить метод суперкласса 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** 

Ответы [ 2 ]

3 голосов
/ 28 октября 2011

Programatically:

    @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 голос
/ 28 октября 2011

Если вы хотите добавить только кнопку, то либо создайте it programatically, либо просто определите только одну кнопку в файле button.xml и установите значение setContentView(R.layout.button);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...