Android的 - 从SurfaceView访问按钮 [英] Android - Accessing buttons from SurfaceView

查看:182
本文介绍了Android的 - 从SurfaceView访问按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个简单的2D塔防游戏,学习一些Android的游戏开发的基础知识。为了处理我的图形我有延伸SurfaceView的自定义对象和我所有的绘图发生在SurfaceView的画布对象。这是所有工作的罚款。

我想我的比赛有两个阶段。在实战阶段和建筑舞台。在建设阶段,我想补充一点,可以单击开始下一阶段的作战按钮。从我的研究,这听起来像按键不能被添加到一个SurfaceView,而是我应该把SurfaceView和相对布局内的按钮(S)和调整的东西的方式。但是,因为我只想按钮显示一半的时候,我和有条件地添加/删除按钮的最好办法挣扎。我可以砍它在那里,但我宁愿把我的时间和半正确如果可能的话做的事情。不幸的是,我一直没能找到这之中没有完成任何很好的例子。

有一个最佳实践的方式来做到这一点,还是我失去了一些东西?这是如何可以处理任何code例子是大大AP preciated。

谢谢!

更新:
我知道我可以使用布局来保存SurfaceView,我想显示的按钮。我的问题是,按钮需要可见只是偶尔。我需要做的是从我的SurfaceView对象设置在我父母的布局按钮的可见性。但是,这是我挣扎的部分。


解决方案

  GLSurfaceView视图=新GLSurfaceView(本);
Button按钮=新按钮(本);
的setContentView(视图);
addContentView(按钮,新的ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));

试试这个code我已经把这样一个TextView只和它的工作

I am currently working on a simple 2d tower defense game to learn some of the basics of Android game development. To handle my graphics I have a custom object that extends SurfaceView and all of my drawing takes place on the SurfaceView's canvas object. This is all working fine.

I'd like my game to have two stages. The "combat" stage and the "building stage". In the building stage, I'm trying to add a button that can be clicked to begin the next combat stage. From my research, it sounds like buttons can't be added to a SurfaceView, but instead I should put the SurfaceView and the button(s) inside of a relative layout and align things that way. However, because I only want the button to show half of the time, I'm struggling with the best way to conditionally add/remove the button. I could hack it in there, but I'd rather take my time and do things semi-correctly if possible. Unfortunately, I haven't been able to find any good examples of this being done yet.

Is there a "best practices" way to do this, or am I missing something? Any code examples of how this can be handled would be greatly appreciated.

Thanks!

Update: I understand that I can use a layout to hold the SurfaceView and the buttons I want to display. My problem is that the buttons need to be visible only occasionally. What I need to do is set the visibility of the buttons in my parent layout from my SurfaceView object. However, this is the part that I am struggling with.

解决方案

GLSurfaceView view = new GLSurfaceView(this);
Button button = new Button(this);
setContentView(view);
addContentView(button,new  ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT ));

try this code i have placed a textview like this only and it worked

这篇关于Android的 - 从SurfaceView访问按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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