2011-12-26 99 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