【问题标题】:Why XML contents not showing on the app when using FrameLayout in Android为什么在 Android 中使用 FrameLayout 时 XML 内容不显示在应用程序上
【发布时间】:2015-06-17 11:28:27
【问题描述】:

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

XML 文件

<?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 代码

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;
}

}

【问题讨论】:

  • 可以提供java代码吗?因为它对我可见
  • 你有LinearLayout只有一个孩子:FrameKayout只有一个孩子:RelativeLayout,没有意义,去掉前两个
  • @Khushal Chouhan:请看我的编辑
  • 您正在以编程方式将 CameraPreview 视图(无论是什么)添加到您的 FrameLayout。由于它是最后添加的,因此它将位于 FrameLayout 中的其他视图之上,这可能会掩盖其下方的视图。

标签: android android-layout android-camera android-xml


【解决方案1】:

您的 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>

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

【讨论】:

    猜你喜欢
    • 2023-02-26
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2020-10-01
    • 2015-11-29
    • 2016-05-16
    • 1970-01-01
    相关资源
    最近更新 更多