简体   繁体   中英

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. 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.

You can't really put TextViews into a SurfaceView. What you can do instead, though, is add TextViews on top of the SurfaceView. 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. Use this documentation for 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. Placing the TextView's on top of the FrameLayout is just a simple matter of configuring the RelativeLayout parameters for each view. You seem new to Android, perhaps this guide will help you with RelativeLayouts and XML layouts in general.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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