简体   繁体   English

SurfaceView中的多个TextView

[英]Multiple TextViews in a SurfaceView

I am creating an android game, I tried using the canvas.drawText() method to display my score and level but it caused errors. 我正在创建一个Android游戏,我尝试使用canvas.drawText()方法显示我的得分和等级,但它导致了错误。 Now I am trying to use TextViews to display them, I am using a SurfaceVeiw and I wanted to know is it possible and would it be a good way of doing it. 现在,我正在尝试使用TextViews来显示它们,我正在使用SurfaceVeiw,我想知道是否有可能,并且这将是一个很好的方法。

You can't really put TextViews into a SurfaceView. 您不能真正将TextViews放入SurfaceView。 What you can do instead, though, is add TextViews on top of the SurfaceView. 但是,您可以做的是在SurfaceView的顶部添加TextViews。 Let me know if you need help on doing that if you don't know how. 如果您不知道该怎么做,请告诉我。

// You can use a FrameLayout to hold the surface view
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(surfaceView);

// Then create a layout to hold everything, for example a RelativeLayout
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(frameLayout);
relativeLayout.addView(textview1);
relativeLayout.addView(textview2);
setContentView(relativeLayout);

When adding your views, it may be helpful to using LayoutParams to help organize things. 添加视图时,使用LayoutParams帮助组织事物可能会有所帮助。 Use this documentation for LayoutParams. 将此文档用于LayoutParams。 Hope this helps! 希望这可以帮助!

If you're using an XML layout, I would use a RelativeLayout, as mentioned above, contain the SurfaceView inside a FrameLayout. 如果您使用的是XML布局,则如上所述,我将使用RelativeLayout,将SurfaceView包含在FrameLayout中。 Placing the TextView's on top of the FrameLayout is just a simple matter of configuring the RelativeLayout parameters for each view. 将TextView放置在FrameLayout之上只是为每个视图配置RelativeLayout参数的简单问题。 You seem new to Android, perhaps this guide will help you with RelativeLayouts and XML layouts in general. 您似乎对Android还是陌生的,也许本指南将在总体上帮助您使用RelativeLayouts和XML布局。

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

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