Android -在一个Activity中有两个setViewContent -一个在xml中,一个在java中



我最近构建了一个应用程序,可以在屏幕上显示相机预览。

package com.street.lamp;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import java.io.IOException;
public class StartingPoint extends Activity {
private Preview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("hoera");
    // Hide the window title.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.main);
    // Create our Preview view and set it as the content of our activity.
    try{
        mPreview = new Preview(this);
        setContentView(mPreview);
        System.out.println("hoera");
        }
    catch(RuntimeException e){
        System.out.println(e.getMessage());
    }
}
}
// ----------------------------------------------------------------------
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
    super(context);
    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    mCamera = Camera.open();
    try {
       mCamera.setPreviewDisplay(holder);
    } catch (IOException exception) {
        mCamera.release();
        mCamera = null;
        // TODO: add more exception handling logic here
    }
}
public void surfaceDestroyed(SurfaceHolder holder) {
    // Surface will be destroyed when we return, so stop the preview.
    // Because the CameraDevice object is not a shared resource, it's very
    // important to release it when the activity is paused.
    mCamera.stopPreview();
    mCamera.release();
    mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Now that the size is known, set up the camera parameters and begin
    // the preview.
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewSize(w, h);
    mCamera.setParameters(parameters);
    mCamera.startPreview();
}
}

上面这是我的主屏幕代码,你可以看到我有setContentView两次。现在,我知道设置两次只会显示最新的,但我还没有找到任何其他解决方案。这是我的main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText android:layout_width="match_parent" android:text="Hello World" android:textColor="@android:color/white" android:focusable="true" android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_weight="0.18" android:background="@android:color/transparent">
    <requestFocus></requestFocus>
</EditText>
</LinearLayout>

我如何包括两个contentviews?

谢谢!

setContentView覆盖您当前活动的整个内容。如果你想要一个包含一些动态组件的布局(比如你的预览),那么预览需要成为主布局的一部分,并在运行时添加到你定义的特定组件中。

类似:

....
setContentView(R.layout.main);
....
final ViewGroup locationForMyPreview = (ViewGroup) findViewById(R.id.preview_location);
mPreview = new Preview(this);
locationForMyPreview.addView(mPreview);
....

这个R.id.preview_location ViewGroup可以定义为主布局中的任何类型的布局(RelativeLayout, LinearLayout,等等)。

你的main.xml变成这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <EditText android:layout_width="match_parent" android:text="Hello World"     
        android:textColor="@android:color/white" android:focusable="true"    
        android:id="@+id/editText1" android:layout_height="wrap_content" 
        android:layout_weight="0.18" android:background="@android:color/transparent">
    <requestFocus></requestFocus>
    </EditText>
    <LinearLayout android:id="@+id/preview_location"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

最新更新