2011-12-26 104 views
1

一個LinearLayout中我有一個名爲面板這樣的類:借鑑SurfaceView

public class Test extends Activity{ 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(new Panel(this)); 
    ... 
} 
class Panel extends SurfaceView implements SurfaceHolder.Callback { 

    private LinearLayout layout; 
    public Panel(Context context) { 
      super(context); 
      layout = //get layout resource in layouts folder 
    } 

    public void onDraw(Canvas canvas) { 
     //I want to draw my xml layout 
     layout.draw(canvas); 

    } 
} 

我曾嘗試使用LayoutInflater爲了得到我的佈局:

LayoutInflater inflater = LayoutInflater.from(context); 
layout = new LinearLayout(getApplicationContext()); 
inflater.inflate(R.layout.mylayout, layout); 

...但我不能看到屏幕上的任何東西,只有一個黑色的背景:S

回答

0

據我所知,你不能畫線性佈局裏面SurfaceView你可以畫一個如果你想像按鈕或類似的東西添加元素到你的視圖中,那麼你需要在SurfaceView之內,我建議使用XML並將所有元素放在那裏。事情是這樣的:

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

    <SurfaceView android:id="@+id/mySurfaceView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1">  
    </SurfaceView> 
    <Button android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:text="myButton"/> 
</LinearLayout> 

,然後你可以將你的視圖的內容與setContentView(R.layout.my_xml);

+0

的一點是,我想先畫一個佈局在屏幕的,然後我想動畫這個佈局爲了顯示它。問題是我不能在屏幕上繪製任何東西,因爲當我將元素帶到屏幕上時沒有顯示。所以我必須使用SurfaceView,就像在這篇文章中評論的一樣:http://stackoverflow.com/questions/2554871/android-how-to-position-view-off-screen,但我無法做到這一點。 – user1116714 2011-12-27 13:19:29