在SurfaceView上绘制LinearLayout

时间:2011-12-26 20:52:00

标签: android layout surfaceview

我有一个叫做Panel的类:

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

1 个答案:

答案 0 :(得分:0)

据我所知,你不能在<{em>内部中绘制线性布局 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);

设置视图内容