2012-04-19 90 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尋求幫助。