布局:膨胀复合视图

时间:2013-12-16 13:20:23

标签: android xml android-layout layout

在我的小型演示应用程序中,我在SurfaceView上显示相机预览。工作良好。但现在我想使用复合XML进行内部预览,这样我就可以将一些文本渲染到相机图片上。屏幕上的布局是:

enter image description here

class CameraPreview extends ViewGroup implements SurfaceHolder.Callback {

    CameraPreview(Context context) {
        super(context);

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

        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
    }

预览在主要活动中创建:

public class CameraActivity extends Activity {

    private CameraPreview mPreview;
    Camera mCamera;
    int numberOfCameras;
    int cameraCurrentlyLocked;
    int defaultCameraId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);

主要活动的简单XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/camera_preview"
    tools:context=".CameraActivity" >

</FrameLayout>

到目前为止一切顺利。但是现在我添加了一个camera_preview.xml,它还包含之前在代码中创建的SurfaceView。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <SurfaceView
        android:id="@+id/renderingView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="blabla" />

</FrameLayout>

当我改为CameraPreview类的构造函数时......

CameraPreview(Context context) {
    super(context);

    String inflaterService = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater li;
    li = (LayoutInflater) context.getSystemService(inflaterService);
    li.inflate(R.layout.camera_preview, this, true);
    mSurfaceView = (SurfaceView) findViewById(R.id.renderingView);

    mHolder = mSurfaceView.getHolder();
    mHolder.addCallback(this);

问题是,生成的屏幕只是白色,我看不到预览文本。但我无法弄清楚我做错了什么。

任何帮助表示感谢。

0 个答案:

没有答案