添加按钮在Android的自定义视图 [英] Add Button on custom View in Android

查看:105
本文介绍了添加按钮在Android的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的类

I have the following class

public class GameActivity extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      View gameView = new GameView(this);
      setContentView(gameView);
   }
}



现在我想将按钮添加到我的课GameView。


and now I'd like to add a Button to my class GameView.

public GameView(Context context) {
        super(context);
// Code ...
}

我在我的游戏过程中需要这个键,所以它应该是在所有其他的帆布我画的前面alywas。
我该怎么办呢?

I need this button during my game, so it should be alywas in front of all the other canvas' I'm drawing.
How can I do that?

推荐答案

你想创建一个新的按钮?

Do you want to create a new button?

Button b = new Button(context);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
                                   LayoutParams.WRAP_CONTENT)); 

gameView.addView(b);

使用的ViewGroup GameView 父insted的简单视图

Use ViewGroup as GameView parent insted of simple View

ViewGroup gameView = new GameView(this);


public class GameView extends ViewGroup { //...

这篇关于添加按钮在Android的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆