将 SurfaceView 与其他 View(例如 TextView 和 Button)组合

发布于 2024-09-29 09:03:48 字数 1374 浏览 5 评论 0原文

我对 Android 编程完全陌生(只是做了一些教程/阅读开发指南等),并且作为每个新手,我都想做一个无用的游戏:-)。

目前我正在努力解决不同视图的布局。与示例类似,我创建了一个扩展 SurfaceView 并将其放入 FrameLayout 的类。围绕这个 SurfaceView 我想要其他视图,例如 Buttons & 文本视图。 像这样的事情:

-----------------------------------------------
| TextView | SurfaceView           | TextView |
|          |                       |          |
------------                       ------------
|          |                       | TextView |
|          |                       |          |
|          |                       ------------
|          |                       | TextView |
|          |                       |          |
|          |                       ------------
|          |                       |          |
|          |                       |          |
-----------------------------------------------
| Button                             Button   |
-----------------------------------------------

我已经成功地使用 FrameLayoutRelativeLayouts 做了类似的事情(将 TextViews 粘贴在屏幕边缘)但我想更好地控制 SurfaceView 的大小,因为它应该是我将在其中绘制的对象的宽度和高度的倍数。我尝试将 layout_widthlayout_height 设置为某些 dp 值,但是当我开始在 0,0 处绘画时,它仍然位于左上角(其中 TextView 是..)。

那么,实现上述布局的最佳实践是什么?使用什么布局?或者我应该更好地在 SurfaceViewdraw() 函数中绘制文本?

I'm totally new to android programming (just did some tutorials/read the dev guides etc.) and as every newbie I want to do a useless game :-).

Currently I'm struggling with the layout of different views. Similar to the example I've made a class which extends a SurfaceView and put that into a FrameLayout. Around this SurfaceView I want to have other View's like Buttons & TextViews.
Something like this:

-----------------------------------------------
| TextView | SurfaceView           | TextView |
|          |                       |          |
------------                       ------------
|          |                       | TextView |
|          |                       |          |
|          |                       ------------
|          |                       | TextView |
|          |                       |          |
|          |                       ------------
|          |                       |          |
|          |                       |          |
-----------------------------------------------
| Button                             Button   |
-----------------------------------------------

I've managed to do something like this with a FrameLayout and RelativeLayouts (sticking the TextViews at the edges of the screen) but I'd like to better control the size of the SurfaceView as it should be a multiple in width and height of the object(s) I'll be drawing in it. I've tried setting layout_width and layout_height to some dp values but when I start painting at 0,0 it's still at the very top-left corner (where the TextView is..).

So, what's the best practice to achieve a layout as above? Using what layout? Or should I better draw my text inside the draw() function of my SurfaceView instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜泪 2024-10-06 09:03:48

布局是将 OpenGL 内容与按钮、标签和其他类型的视图对象组合的正确方法。您可以在其他布局中组合布局...因此您可以结合线性布局或您喜欢的任何布局逐步构建屏幕。

例如,您可以使用相对布局来设置按钮(b1、b2)和屏幕的其余部分:

+-----------+
|  Zone 1   |
+-----+-----+
| b1  | b2  |
+-----+-----+

然后在区域 1 内,您可以对 3 个主要列使用水平线性布局

+----+----+----+
| c1 | c2 | c3 |
+----+----+----+

在 c2 内,您可以放置​​ SurfaceView ,在 c1 中,c3 内的文本标签

将是一个新的垂直线性布局来显示文本标签。

+----+
| t1 |
+----+
| t2 |
+----+
| t3 |
+----+

Layouts are the right way to combine OpengGL content with buttons, labels and other type of view objects. You can combine layouts inside other layouts... so you may build your screen step by step combining Linear Layouts or whatever you prefer.

For example, you can use a Relative layout to setup the buttons (b1, b2) and the rest of the screen:

+-----------+
|  Zone 1   |
+-----+-----+
| b1  | b2  |
+-----+-----+

Then inside Zone 1, you can use a Horizontal Linear Layout for the 3 main columns

+----+----+----+
| c1 | c2 | c3 |
+----+----+----+

Inside of c2 you can place the SurfaceView, and in c1 the text label

Inside c3 will be a new Vertical Linear layout to display the text labels.

+----+
| t1 |
+----+
| t2 |
+----+
| t3 |
+----+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文