为什么在Android中使用FrameLayout时不会在应用上显示XML内容

时间:2022-11-03 21:18:13

In my app I have created a custom camera app ,using FrameLayout in Xml. And I have a capture button to capture images,and two other buttons. These buttons are arranged inside a RelativeLayout inside FrameLayout.But it is not displaying when running my App. Any solution for this problem. Can anybody help?

在我的应用程序中,我使用Xml中的FrameLayout创建了一个自定义相机应用程序。我有一个捕捉按钮来捕捉图像,还有另外两个按钮。这些按钮排列在FrameLayout内的RelativeLayout内部。但是在运行我的应用程序时它没有显示。这个问题的任何解决方案。有人可以帮忙吗?

XML file

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

<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1">

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    <ImageButton
        android:id="@+id/buttonLoadPicture2"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:padding="6dp"
        android:scaleType="fitCenter"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"/>



    <ImageButton
        android:id="@+id/button_capture"
        android:layout_width="90dip"
        android:layout_height="50dip"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"

        android:layout_marginTop="0dp"
        android:layout_toRightOf="@+id/buttonLoadPicture2"
        android:layout_alignParentBottom="true"
        android:src="@drawable/camera"
        android:layout_marginRight="5dp"



        />
    <Button android:id="@+id/buttonLoadPicture3"
        android:layout_width="90dip"
        android:layout_height="50dip"
        android:layout_alignParentBottom="true"
        android:layout_gravity="end"
        android:layout_toRightOf="@+id/button_capture"

        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="0dp"
        android:text="Cancel"
        ></Button>
    </RelativeLayout>

</FrameLayout>
</LinearLayout>

JAVA CODE

public class CameraActivity extends Activity {

private Camera mCamera;
private CameraPreview mPreview;
public static final int MEDIA_TYPE_IMAGE = 1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
    System.out.println("Starting!");

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

    final Camera.PictureCallback mPicture = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

            if (pictureFile == null){
                return;
            }

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                MediaStore.Images.Media.insertImage(getContentResolver(), pictureFile.getAbsolutePath(), pictureFile.getName(), pictureFile.getName());
            } catch (FileNotFoundException e) {

            } catch (IOException e) {

            }
        }
    };





    // Add a listener to the Capture button
    captureButton.setOnClickListener(

            new View.OnClickListener() {

                public void onClick(View v) {
                    // get an image from the camera

                    System.out.println("Photo Taking!");
                    mCamera.takePicture(null, null, mPicture);



                }
            }
    );



}




/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

@Override
protected void onPause() {
    super.onPause();
    releaseCamera();              // release the camera immediately on pause event
}



private void releaseCamera(){
    if (mCamera != null){
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}



/** Create a File for saving an image or video */
private  File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyCameraApp");


    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

}

1 个解决方案

#1


0  

Your FrameLayout covers the entire screen and you are adding your camera preview to it which will be added on top of the buttons and therefore cover them. A quick fix would be move all your buttons out of the frame layout so your layout looks like this.

你的FrameLayout覆盖了整个屏幕,你正在添加你的相机预览,它将添加到按钮的顶部,因此覆盖它们。快速解决方法是将所有按钮移出框架布局,以便您的布局如下所示。

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

    <FrameLayout
            android:id="@+id/camera_preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">


    </FrameLayout>
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        <ImageButton
                android:id="@+id/buttonLoadPicture2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="0dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:padding="6dp"
                android:scaleType="fitCenter"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"/>



        <ImageButton
                android:id="@+id/button_capture"
                android:layout_width="90dip"
                android:layout_height="50dip"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"

                android:layout_marginTop="0dp"
                android:layout_toRightOf="@+id/buttonLoadPicture2"
                android:layout_alignParentBottom="true"
                android:src="@android:drawable/ic_menu_camera"
                android:layout_marginRight="5dp"/>

        <Button android:id="@+id/buttonLoadPicture3"
                android:layout_width="90dip"
                android:layout_height="50dip"
                android:layout_alignParentBottom="true"
                android:layout_gravity="end"
                android:layout_toRightOf="@+id/button_capture"

                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="0dp"
                android:text="Cancel"
                ></Button>
    </RelativeLayout>
</LinearLayout>

Note that the above will keep your camera preview above the relative layout containing the buttons. So if you are trying to have the buttons on top your preview you can retain your old code/layout and use brintToFront()

请注意,以上操作会使相机预览保持在包含按钮的相对布局之上。因此,如果您尝试将按钮放在预览的顶部,则可以保留旧代码/布局并使用brintToFront()

#1


0  

Your FrameLayout covers the entire screen and you are adding your camera preview to it which will be added on top of the buttons and therefore cover them. A quick fix would be move all your buttons out of the frame layout so your layout looks like this.

你的FrameLayout覆盖了整个屏幕,你正在添加你的相机预览,它将添加到按钮的顶部,因此覆盖它们。快速解决方法是将所有按钮移出框架布局,以便您的布局如下所示。

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

    <FrameLayout
            android:id="@+id/camera_preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">


    </FrameLayout>
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        <ImageButton
                android:id="@+id/buttonLoadPicture2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="0dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:padding="6dp"
                android:scaleType="fitCenter"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"/>



        <ImageButton
                android:id="@+id/button_capture"
                android:layout_width="90dip"
                android:layout_height="50dip"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"

                android:layout_marginTop="0dp"
                android:layout_toRightOf="@+id/buttonLoadPicture2"
                android:layout_alignParentBottom="true"
                android:src="@android:drawable/ic_menu_camera"
                android:layout_marginRight="5dp"/>

        <Button android:id="@+id/buttonLoadPicture3"
                android:layout_width="90dip"
                android:layout_height="50dip"
                android:layout_alignParentBottom="true"
                android:layout_gravity="end"
                android:layout_toRightOf="@+id/button_capture"

                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="0dp"
                android:text="Cancel"
                ></Button>
    </RelativeLayout>
</LinearLayout>

Note that the above will keep your camera preview above the relative layout containing the buttons. So if you are trying to have the buttons on top your preview you can retain your old code/layout and use brintToFront()

请注意,以上操作会使相机预览保持在包含按钮的相对布局之上。因此,如果您尝试将按钮放在预览的顶部,则可以保留旧代码/布局并使用brintToFront()