【问题标题】:How to add a layout to this如何为此添加布局
【发布时间】:2012-04-19 13:35:29
【问题描述】:

我的游戏中有一个课程是从教程中复制而来的:

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),但在pt 中未定义addView。是否可以修改 PanelThing,可能使用“扩展”或“实现”以便定义 addView?还是有其他方法可以向 pt 添加布局?

编辑:如果您想知道我为什么要为 pt 设置子布局,请参阅 this question

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    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>
    

    【讨论】:

      【解决方案2】:

      你不能在 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>
      

      您可以将您的横幅添加到可以放置在您喜欢的任何位置的线性布局中。希望这个解决方案有所帮助。您可以查看this thread 寻求帮助。

      【讨论】:

        猜你喜欢
        • 2015-10-26
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-21
        相关资源
        最近更新 更多