如何在屏幕上显示按钮?
所以我正在为 Android 制作一款游戏,基本上涉及一艘太空飞船在屏幕上左右移动(锁定在纵向模式),试图躲避从屏幕顶部飞向它的物体。我有一个 GraphicsView 类(它扩展了 View),用于处理所有绘图等。我试图在按钮上显示两个按钮,一个将船移动到左侧,一个将船移动到右侧。我的代码遍布各处:
Button mLeft;
mLeft = new Button (context);
mLeft.setX (0);
mLeft.setY (mDisplay.getHeight () - 64);
mLeft.setHeight (64);
mLeft.setWidth (64);
mLeft.setBackgroundColor (80000000);
mLeft.setText ("<");
每当我尝试调用上面的 mLeft 的任何方法时,我的程序都会崩溃。我尝试将它们全部注释掉,然后将它们单独添加回来,看看它们中的任何一个是否不会崩溃,但它们都会崩溃。我可以在程序不崩溃的情况下执行 mLeft.draw (画布),但屏幕上没有显示任何内容。
对此的任何帮助将不胜感激,谢谢。
So I am making a game for Android that basically involves a space ship moving left and right on the screen (locked in portrait mode) that is trying to dodge objects flying at it from the top of the screen. I have a class GraphicsView (it extends View) that deals with all of the drawing and whatnot. I am trying to display two buttons on the button, one to move the ship to the left and one to move the ship to the right. I have this code strewn throughout:
Button mLeft;
mLeft = new Button (context);
mLeft.setX (0);
mLeft.setY (mDisplay.getHeight () - 64);
mLeft.setHeight (64);
mLeft.setWidth (64);
mLeft.setBackgroundColor (80000000);
mLeft.setText ("<");
Whenever I try to call any of these methods of mLeft above, my program crashes. I have tried commenting all of them out, then adding them back individually to see if any one of them won't crash, but ll of them do. I am able to do mLeft.draw (canvas) without the program crashing, but nothing shows up on the screen.
Any help on this would be very appreciated, thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您添加了
mLeft.setOnClickListener()
吗?另外,对于onDraw()
,您是否在方法末尾添加了invalidate()
?did you add
mLeft.setOnClickListener()
. Also, for theonDraw()
, did you addinvalidate()
at the end of the method?这可能是因为 setText("<")。尝试将其更改为其他内容。
It could be because of setText("<"). Try changing that to something else.