2012-04-19 91 views
0

我有我的游戏中的一类,我从一个教程复制:如何布局添加到此

public class PanelThing extends SurfaceView implements SurfaceHolder.Callback 
{ 
} 

我从来没有完全理解这个类是如何工作的,但似乎做的工作,我的游戏一段时间以来一直很好。这个PanelThing似乎有点像某种布局。在我的游戏的onCreate方法中,我有:

pt = new PanelThing(this); 
game_play_layout = new LinearLayout(this); 
game_play_layout.addView(pt); 
setContentView(game_play_layout); 

现在我需要在pt内有一个子布局。我试着做pt.addView(my_child_layout),但addView在pt中是未定义的。是否有可能修改PanelThing,也许用“扩展”或“实现”,使addView变得定义?或者是否有其他方式可以向pt添加布局?

编辑:如果你想知道为什么我想要一个pt的孩子布局,请参阅this question

回答

1

a SurfaceView不是像LinearLayout那样的布局容器,因此您无法为其添加视图。这更像是您随时在代码中绘制的交互式图像。

您必须将您要显示的附加信息添加到您的SurfaceView的图纸代码中,或者您可以使用FrameLayout将其排列在SurfaceView之上。确保顶部的布局在您不想阻挡下面内容的区域是透明的。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <PanelThing 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    < -- Layout on top here. -- 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 
1

那么你不能在SurfaceView中有子元素。一种可能的解决方案可以是做类似的事情。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <PanelThing 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <LinearLayout android:visibility="GONE"> 
    </LinearLayout> 
</RelativeLayout> 

而且您可以将横幅添加到LinearLayout,并将其放置在任何位置。希望这个解决方案有帮你可以看到this thread寻求帮助。