将 SurfaceView 与其他 View(例如 TextView 和 Button)组合
我对 Android 编程完全陌生(只是做了一些教程/阅读开发指南等),并且作为每个新手,我都想做一个无用的游戏:-)。
目前我正在努力解决不同视图的布局。与示例类似,我创建了一个扩展 SurfaceView 并将其放入 FrameLayout 的类。围绕这个 SurfaceView
我想要其他视图,例如 Buttons
& 文本视图
。 像这样的事情:
-----------------------------------------------
| TextView | SurfaceView | TextView |
| | | |
------------ ------------
| | | TextView |
| | | |
| | ------------
| | | TextView |
| | | |
| | ------------
| | | |
| | | |
-----------------------------------------------
| Button Button |
-----------------------------------------------
我已经成功地使用 FrameLayout
和 RelativeLayouts
做了类似的事情(将 TextViews
粘贴在屏幕边缘)但我想更好地控制 SurfaceView 的大小,因为它应该是我将在其中绘制的对象的宽度和高度的倍数。我尝试将 layout_width
和 layout_height
设置为某些 dp 值,但是当我开始在 0,0 处绘画时,它仍然位于左上角(其中 TextView 是..)。
那么,实现上述布局的最佳实践是什么?使用什么布局?或者我应该更好地在 SurfaceView
的 draw()
函数中绘制文本?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
布局是将 OpenGL 内容与按钮、标签和其他类型的视图对象组合的正确方法。您可以在其他布局中组合布局...因此您可以结合线性布局或您喜欢的任何布局逐步构建屏幕。
例如,您可以使用相对布局来设置按钮(b1、b2)和屏幕的其余部分:
然后在区域 1 内,您可以对 3 个主要列使用水平线性布局
在 c2 内,您可以放置 SurfaceView ,在 c1 中,c3 内的文本标签
将是一个新的垂直线性布局来显示文本标签。
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:
Then inside Zone 1, you can use a Horizontal Linear Layout for the 3 main columns
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.