简体   繁体   English

在没有 XML 或布局的情况下向活动添加按钮

[英]Adding a button to activity without XML or Layout

Android Studio Android工作室

We have a project (game) that we need to do android game, a space racing game and we're almost done.我们有一个项目(游戏),我们需要做 android 游戏,一个太空赛车游戏,我们几乎完成了。 We just need to add a retry button and exit button on the game view.我们只需要在游戏视图上添加一个重试按钮和退出按钮。 The code below is on my GamePlayScene.java and it doesn't have a XML file on the layout folder.下面的代码在我的 GamePlayScene.java 上,并且布局文件夹中没有 XML 文件。 They said that it is connected through the game itself.他们说它是通过游戏本身连接起来的。 I'm super new to coding a game and on Java language.我对游戏编码和 Java 语言非常陌生。 When I press the start button on the emulator it goes to MainActivty2.java and that MainActivity2 has a XML or on the layout folder but it doesn't have any designs on it, it doesn't have spaceship or anything that we can see if we run the game or start the game on the emulator.当我按下模拟器上的开始按钮时,它会转到 MainActivty2.java 并且 MainActivity2 有一个 XML 或布局文件夹,但它上面没有任何设计,它没有宇宙飞船或任何我们可以看到的东西我们在模拟器上运行游戏或启动游戏。 So I tried to add a button on that XML and when I tried to press the start button, the message appears "Unfortunately, SpaceRacing has stopped".因此,我尝试在 XML 上添加一个按钮,当我尝试按下开始按钮时,出现消息“不幸的是,SpaceRacing 已停止”。 I hope someone can understand me with this.我希望有人能理解我。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    Button button= new Button (this);
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT);
    params.topMargin = 0;
    params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;

    button.setText("Retry");
    addContentView(button, params);
    // setContentView(tv);
    button.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(this, MainActivity2.class);
        }

    });
}

The bold letter or has " ** ** " on the code has a red font on the studio.粗体字母或代码上有“** **”的工作室有红色字体。

You need to start with a setContentView so your activity will have an initial layout.您需要从setContentView开始,以便您的活动具有初始布局。

To that method you will pass either a view or a layout id.对于该方法,您将传递视图或布局 ID。

So if for example you have a layout file named activity2_layout.xml your onCreate method should look something like this因此,例如,如果您有一个名为activity2_layout.xml的布局文件,您的onCreate方法应该如下所示

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2_layout)

And then you call your button with然后你调用你的按钮

findViewById(R.id.buttun_id)

And if you want to add the button there without XML, you first find your layout and then call如果你想在没有 XML 的情况下添加按钮,你首先找到你的布局然后调用

myLayout.addView(button)

This is really Android basics, you probably want to take a look at this before proceeding这真的是 Android 基础知识,您可能想在继续之前先看看这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM