2013-06-11 94 views
0

我已經實現zbar和到現在我想在一個活動是條形碼掃描儀,並沒有任何相關的佈局Oncreate方法編程方式創建一個按鈕 ..所以我使用此代碼:創建一個按鈕編程的onCreate

LinearLayout layout = new LinearLayout(this); 
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

Button button = new Button(this); 
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
button.setText("Back"); 
layout.addView(button); 
setContentView(layout); 

除相機屏幕外沒有任何內容顯示。

我有添加別的東西嗎?謝謝:)

編輯:條碼讀取代碼:

public class ZBarScannerActivity extends Activity implements Camera.PreviewCallback, ZBarConstants { 

private static final String TAG = "ZBarScannerActivity"; 
private CameraPreview mPreview; 
private Camera mCamera; 
private ImageScanner mScanner; 
private Handler mAutoFocusHandler; 
private boolean mPreviewing = true; 

private ProgressDialog progress; 
public static String MsgErr = null; 
public static String BARCODE; 

static { 
    System.loadLibrary("iconv"); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    LinearLayout layout = new LinearLayout(this); 
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

    Button button = new Button(this); 
    button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    button.setText("Back"); 
    layout.addView(button); 
    setContentView(layout); 

    if(!isCameraAvailable()) { 
     // Cancel request if there is no rear-facing camera. 
     cancelRequest(); 
     return; 
    } 

    mAutoFocusHandler = new Handler(); 

    // Create and configure the ImageScanner; 
    setupScanner(); 

    // Create a RelativeLayout container that will hold a SurfaceView, 
    // and set it as the content of our activity. 
    mPreview = new CameraPreview(this, this, autoFocusCB); 
    setContentView(mPreview); 
} 
+0

與此代碼,你會看到屏幕上的按鈕。你沒有看到它? –

+0

謝謝。不,我除了相機屏幕以外沒有看到任何東西。 – Copernic

+0

相機屏幕?請發佈您的完整代碼。 – Oam

回答

2

調用2倍的setContentView,它具有替代的佈局效果。

使在順序

替換這一行這些變化:

setContentView(layout); 

由:

layout.setOrientation(LinearLayout.VERTICAL); 

和替換:

setContentView(mPreview); 

由:

layout.addView(mPreview); 
setContentView(layout); 

現在你應該看到兩個元素

+0

謝謝。但現在我再也看不到相機屏幕了...... – Copernic

+0

我沒有試過這段代碼,但可以肯定的是問題在於佈局的管理。 –

+0

是的,它看起來像按鈕所在的佈局,不是作爲相機屏幕之一的sa * me。您對此有任何想法嗎? – Copernic

相關問題