Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将布局添加到此_Java_Android_Eclipse - Fatal编程技术网

Java 如何将布局添加到此

Java 如何将布局添加到此,java,android,eclipse,Java,Android,Eclipse,我在游戏中有一个从教程中复制的类: public class PanelThing extends SurfaceView implements SurfaceHolder.Callback { } 我从来都不太明白这门课是如何运作的,但它似乎完成了这项工作,而且我的游戏已经运行了一段时间了。这个面板似乎有点像某种布局。在我的游戏的onCreate方法中,我有: pt = new PanelThing(this); game_play_layout = new LinearLayout(thi

我在游戏中有一个从教程中复制的类:

public class PanelThing extends SurfaceView implements SurfaceHolder.Callback
{
}
我从来都不太明白这门课是如何运作的,但它似乎完成了这项工作,而且我的游戏已经运行了一段时间了。这个面板似乎有点像某种布局。在我的游戏的onCreate方法中,我有:

pt = new PanelThing(this);
game_play_layout = new LinearLayout(this);
game_play_layout.addView(pt);
setContentView(game_play_layout);
现在我需要在pt中有一个子布局。我试着做了
pt.addView(我的孩子的布局)
,但是在pt中addView没有定义。是否可以通过“扩展”或“实现”修改PanelThing,以便定义addView?或者是否有其他方法可以将布局添加到pt


编辑:如果您想确切了解为什么我需要pt的子布局,请参阅。

好的,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,可以将其放置在您喜欢的任何位置。希望这个解决方案有帮助。您可以查看以获得帮助。

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>

<--此处顶部的布局--
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”/>