0

我已经实现了ZBar,现在我想以编程方式在一个活动的方法上创建一个按钮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);
}
4

2 回答 2

1

你调用了2次setContentView,有替换布局的效果。

按顺序进行这些更改

替换这一行:

setContentView(layout);

经过:

layout.setOrientation(LinearLayout.VERTICAL);

并替换:

setContentView(mPreview);

经过:

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

现在你应该看到这两个元素

于 2013-06-11T21:20:15.027 回答
0
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);

//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);

//add button to the layout
layout.addView(btnTag);
于 2019-01-16T09:01:23.263 回答