Android布局:如何将此ViewGroup放在RelativeLayout中?

时间:2012-03-13 03:12:39

标签: android

您好如何使此代码有效?我希望这个类CameraSurfaceView放在一个相对布局?

代码:

public class CameraSurfaceView extends ViewGroup  implements SurfaceHolder.Callback{
    private final String TAG = "Preview";

    private SurfaceView mSurfaceView;
    private SurfaceHolder mHolder;
    private Size mPreviewSize;
    private List<Size> mSupportedPreviewSizes;
    private Camera mCamera;

    public CameraSurfaceView(Context context) {
        super(context);

        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);

        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }
     ...
}

MainActivity;

public class MainActivity extends Activity{
    RelativeLayout rlCamWrapper;
    CameraSurfaceView cameraSurfaceView;


    private SurfaceView surfaceView;

    private boolean isRecording = false;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);


        cameraSurfaceView = new CameraSurfaceView(this);

        setContentView(R.layout.main);
    }
    ...
}

的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/relativeLayout1"
    android:orientation="horizontal"
    android:weightSum="1.0"
    >

    <VideoView 
        android:id="@+id/videoView1" 
        android:layout_alignParentTop="true"
        android:layout_height="fill_parent" android:layout_width="fill_parent"
        android:layout_weight=".5"/>

    <RelativeLayout 
        android:id="@+id/surfaceViewWrapper" 
        android:layout_alignParentTop="true"
        android:layout_height="fill_parent" android:layout_width="fill_parent"
        android:layout_weight=".5">

        <SurfaceView 
            android:id="@+id/surfaceView2" 
            android:layout_alignParentTop="true"
            android:layout_height="fill_parent" android:layout_width="fill_parent"
            />

    </RelativeLayout>
</LinearLayout>

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

只需使用<SurfaceView更改xml中的<your.package.CameraSurfaceView即可。然后在代码中使用findViewById()获取它

public class MainActivity extends Activity{
    RelativeLayout rlCamWrapper;
    CameraSurfaceView cameraSurfaceView;


    private SurfaceView surfaceView;

    private boolean isRecording = false;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        // this is the important part
        cameraSurfaceView = (CameraSurfaceView) findViewById(R.id.surfaceView2);
    }
    ...
}

另外,请确保为您的视图重载其他构造函数(如下所示):

public class CameraSurfaceView extends ViewGroup  implements SurfaceHolder.Callback{
    private final String TAG = "Preview";

    private SurfaceView mSurfaceView;
    private SurfaceHolder mHolder;
    private Size mPreviewSize;
    private List<Size> mSupportedPreviewSizes;
    private Camera mCamera;

    public CameraSurfaceView(Context context) {
        super(context);

        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);

        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    public CameraSurfaceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        ...
    }

    public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        ...
    }
     ...
}

答案 1 :(得分:0)

无需创建实例

       cameraSurfaceView = new CameraSurfaceView(this);

只需通过setContentView设置布局。还在你的xml中设置了

      <SurfaceView in your xml with <your.package.CameraSurfaceView

你完成了。

相关问题