2014-02-23 77 views
0

我正在使用ViewPager,我有兩個片段。第一個片段顯示了CameraPreview。我想在這個片段上面包含另一個佈局,但我只能將此佈局添加到活動中(它顯示了兩個片段之上的佈局,而不僅僅是第一個片段)。Android中SurfaceView的佈局

這就是我所擁有的。但Button和TextView也顯示在父Activity的另一個片段中。

我使用此代碼添加覆蓋佈局(R.layout.control)

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     autoFocusHandler = new Handler(); 
     mCamera = getCameraInstance(); 

     /* Instance barcode scanner */ 
     scanner = new ImageScanner(); 
     scanner.setConfig(0, Config.X_DENSITY, 3); 
     scanner.setConfig(0, Config.Y_DENSITY, 3); 

     mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB); 

     dialogProduct = new Dialog(getActivity(), R.style.DialogSlideAnim); 
     dialogProduct.setContentView(R.layout.dialog_register); 

    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_scan, container, false); 

     FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.cameraPreviewFrame); 
     preview.addView(mPreview); 

     scanText = (TextView) rootView.findViewById(R.id.scanText); 

     scanButton = (Button) rootView.findViewById(R.id.ScanButton); 

     scanButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       if (barcodeScanned) { 
        barcodeScanned = false; 
        scanText.setText("Scanning..."); 
        mCamera.setPreviewCallback(previewCb); 
        mCamera.startPreview(); 
        previewing = true; 
        mCamera.autoFocus(autoFocusCB); 
       } 
      } 
     }); 

     //Add overlay layout 
     View headerView = View.inflate(getActivity(), R.layout.control, null); 
     LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     getActivity().addContentView(headerView,layoutParamsControl); 

     return rootView; 

    } 

這是片段的佈局

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

    <TextView 
     android:id="@+id/totalText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="@string/total_text" 
     android:textSize="30sp" > 
    </TextView> 

    <FrameLayout 
     android:id="@+id/cameraPreviewFrame" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 
    </FrameLayout> 

    <TextView 
     android:id="@+id/scanText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/escaneando" > 
    </TextView> 

    <Button 
     android:id="@+id/ScanButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/btn_scan" /> 

</LinearLayout> 

問題

  1. 添加後我試過使用方法addView()mPreview(CameraPreview)但我得到一個錯誤,因爲我不刪除以前的觀點。 ¿在片段類中是否有像addContentView(它不會刪除先前的視圖)的方法?

  2. ¿您有任何其他建議可以解決此問題嗎?

我真的很感激你的幫助

謝謝

回答

0

我解決了我的問題,修改了this example。我用一個構造函數修改我的CameraPreview視圖,允許我在FrameLayoutCameraPreview(Context,AttributeSet))中添加一個實例。然後我只是在CameraPreview標籤後添加另一個覆蓋視圖。現在我沒有使用addContentView方法,因爲我可以使用父視圖中的findViewById獲取此視圖。我希望這有助於某人。

I)片段佈局

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

    <FrameLayout 
     android:id="@+id/cameraPreviewFrame" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <com.easyshopping.activities.CameraPreview 
      android:id="@+id/cameraSurfaceView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
     </com.easyshopping.activities.CameraPreview> 

     <Button 
      android:id="@+id/btnTestSurface" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:paddingTop="10dp" 
      android:text="@string/test_button" /> 
    </FrameLayout> 

    <TextView 
     android:id="@+id/scanText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/escaneando" > 
    </TextView> 

    <Button 
     android:id="@+id/ScanButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/btn_scan" /> 

</LinearLayout> 

II)OnCreateView()在該片段中

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_scan, container, false); 

     mPreview = (CameraPreview) rootView.findViewById(R.id.cameraSurfaceView); 
     // Install a SurfaceHolder.Callback so we get notified when the 
     // underlying surface is created and destroyed. 
     mHolder = mPreview.getHolder(); 
     mHolder.addCallback(this); 

     // deprecated setting, but required on Android versions prior to 3.0 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

     // FrameLayout preview = (FrameLayout) 
     // rootView.findViewById(R.id.cameraPreviewFrame); 
     // preview.addView(mPreview); 

     scanText = (TextView) rootView.findViewById(R.id.scanText); 

     scanButton = (Button) rootView.findViewById(R.id.ScanButton); 

     scanButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       if (barcodeScanned) { 
        barcodeScanned = false; 
        scanText.setText("Scanning..."); 
        mCamera.setPreviewCallback(previewCb); 
        mCamera.startPreview(); 
        previewing = true; 
        mCamera.autoFocus(autoFocusCB); 
       } 
      } 
     }); 

     return rootView; 
} 
0

你不能把一個佈局上SurfaceView。您可以讓佈局和SurfaceView處於相同的佈局/活動

要達到您想要的效果,您需要在其佈局中創建新的活動在xml中添加Surfaceview並添加另一個視圖...像這樣

<?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"> 
<Button 
android:id="@+id/dummy" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="a Button" /> 
<FrameLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<com.exercise.AndroidMergeSurfaceView.MySurfaceView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 
</FrameLayout> 
</LinearLayout> 
+0

我很抱歉,但是這不是我的問題。我已經解決了它的一個活動,但問題是與片段一樣。我無法將CameraPreview(SurfaceView的擴展)添加到佈局,因爲它正在實現SurfaceHolder.Callback –