添加从SurfaceView扩展到布局的视图会给我一个空白屏幕

时间:2010-06-05 21:41:30

标签: android 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>

2 个答案:

答案 0 :(得分:2)

什么是game.test.gameEngine?它是否扩展了View类?如果是这样,它将占据整个屏幕。您将宽度和高度都设置为“fill_parent”,这意味着按钮和日期选择器不在屏幕上。如果您希望按钮显示在游戏引擎视图的顶部,则您必须将按钮设置为游戏引擎的子项,或者您必须将高度和宽度设置为将为按钮留出空间的值

嵌入和排列视图可能很难学习,因此我建议您查找类似于您正在执行的代码示例,以及浏览Google在其网站上发布的教程。

答案 1 :(得分:1)

我绝对建议您查看相对布局,如果您打算在主要游戏视图中使用其他内容,例如顶部的按钮。我有这样做的GLSurfaceView,效果非常好。

使用相对布局的一般想法是,如果没有给出任何布局参数,则给出的每个视图都在左上角绘制。但是,当被告知要在父母的某个地方或旁边(在左侧/右侧或上方/下方)对齐另一个视图时,您可以控制出现的位置。

同样值得注意的是,列出的每个新视图都显示在另一个视图的顶部(如果没有另外对齐),那么您首先列出主要的表面游戏视图以及之后的按钮。