Android 添加内容视图后缺少按钮

Android button missing after add content view

我有一个带有几个按钮的框架布局:

setContentView(R.layout.main_layout);

我调用这个相机函数后,

addContentView(mPreview, newFrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

它完全屏蔽了我的 XML 布局。我想要按钮位于相机视图的顶部,我该怎么做?

这是我的相机class:

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

Camera mCamera;
boolean isPreviewRunning = false;

CameraSurfaceView(Context context) {
    super(context);
    SurfaceHolder mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    synchronized(this) {
        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.e("Camera", "mCamera.setPreviewDisplay(holder);");
        }
        mCamera.setDisplayOrientation(90);
        mCamera.startPreview();

    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    synchronized(this) {
        try {
            if (mCamera!=null) {
                mCamera.stopPreview();
                isPreviewRunning=false;
                mCamera.release();
            }
        } catch (Exception e) {
            Log.e("Camera", e.getMessage());
        }
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

}

}

这是我的 XML 布局:

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

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Background"
    android:id="@+id/background"
    android:onClick="background"
    android:layout_gravity="left|bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Camera"
    android:id="@+id/camera"
    android:onClick="camera"
    android:layout_gravity="right|bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:id="@+id/save"
    android:onClick="save"
    android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>

这是因为最后添加的视图在框架布局中位于顶部。

我建议您在布局开始时在您的框架布局中制作一个容器(任何布局),并将您的相机视图放入该容器中。

类似于:

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/cameracontainer"></LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Background"
    android:id="@+id/background"
    android:onClick="background"
    android:layout_gravity="left|bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Camera"
    android:id="@+id/camera"
    android:onClick="camera"
    android:layout_gravity="right|bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:id="@+id/save"
    android:onClick="save"
    android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>

并在 activity 中:

LinearLayout layout= (LinearLayout )findViewById(R.id.cameraContainer);
layout.addView(mPreview);