添加从 SurfaceView 延伸到布局的视图给我一个空白屏幕
我对 Android 编程非常陌生,所以这可能是非常基本的东西。我只有一个带有几个按钮的 xml 布局。我试图遵循 JetBoy 演示给出的模型,因此我向扩展 SurfaceView 的布局添加了一个视图。当这个新视图放入我的 xml 布局中时,我只得到一个空白屏幕。
如果有帮助的话,这是 XML 布局。 gameview 元素是导致屏幕空白的原因
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alwaysDrawnWithCache="true">
<game.test.gameEngine
android:id="@+id/gameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button android:text="Easy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/EasyButton"></Button>
<Button android:text="Medium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/MedButton"></Button>
<Button android:text="Hard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/HardButton"></Button>
<DatePicker android:id="@+id/DatePicker01" android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker><Button android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Exit"></Button>
</LinearLayout>
I'm very new to Android programming, so this is probably something pretty basic. I just have an xml layout with a few buttons. I'm trying to follow the model given by the JetBoy demo, so I'm adding a view to the layout which extends SurfaceView. When this new view is put in my xml layout, I just get a blank screen.
Here's the XML layout if it helps. The gameview element is what causes the screen to be blank
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alwaysDrawnWithCache="true">
<game.test.gameEngine
android:id="@+id/gameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button android:text="Easy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/EasyButton"></Button>
<Button android:text="Medium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/MedButton"></Button>
<Button android:text="Hard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/HardButton"></Button>
<DatePicker android:id="@+id/DatePicker01" android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker><Button android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Exit"></Button>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
什么是游戏.测试.游戏引擎?它扩展了 View 类吗?如果是这样,则它占据了整个屏幕。您将宽度和高度设置为“fill_parent”,这意味着按钮和日期选择器位于屏幕之外。如果您希望按钮出现在游戏引擎视图的顶部,则必须将按钮设置为游戏引擎的子级,或者必须将高度和宽度设置为为按钮留出空间的值。
嵌入和排列视图学习起来可能很困难且复杂,所以我建议寻找与您正在做的事情类似的代码示例,以及浏览谷歌在其网站上发布的教程。
what is game.test.gameEngine? Does it extend the View class? If so, it is taking up the entire screen. you have both width and height set to "fill_parent", which means that the buttons and datepicker are off the screen. If you want the buttons to appear on top of the game engine view, then you will have to either set the buttons as children of the game engine, or you will have to set the height and width to values that will leave space for the buttons.
embedding and arranging views can be difficult and complicated to learn, so i would suggest looking for code examples similar to what you are doing, as well as browsing the tutorials posted by google on their site.
如果您打算在主游戏视图中添加其他内容(例如顶部的按钮),我绝对建议您研究相对布局。我用这种方法做了一个 GLSurfaceView 并且效果很好。
相对布局的总体思想是,在没有给出任何布局参数的情况下,给定的每个视图都绘制在左上角。但是,当被告知要在父视图中的某个位置或另一个视图旁边(左/右,或上方/下方)对齐时,您可以控制事物出现的位置。
值得注意的是,列出的每个新视图都显示在另一个视图的顶部(如果没有对齐的话),因此您可以首先列出主表面游戏视图,然后列出其后的按钮。
I would absolutely suggest looking into a Relative Layout if you're intending on having a main game view with other things like buttons on top. I have a GLSurfaceView done this way and it works very well.
The general idea with a Relative Layout, is that without any layout parameters given, each view given is drawn in the upper left. However, when told to be aligned somewhere in the parent or next to (to the left/right of, or above/below) another view you can control where things appear.
It's also good to note that each new view listed appears on top of the other (if not aligned otherwise) so you'd list the main surface game view first and the buttons after it.