以编程方式在创建上创建一个按钮

时间:2013-06-11 20:32:43

标签: android button layout camera

我已经实现了 ZBar ,现在我确实希望以编程方式在活动的Oncreate方法上创建一个按钮,这是一个条形码扫描程序并且不会t有任何相关的布局..所以我正在使用这段代码:

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

2 个答案:

答案 0 :(得分:1)

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

按顺序进行这些更改

替换此行:

setContentView(layout);

由:

layout.setOrientation(LinearLayout.VERTICAL);

并替换:

setContentView(mPreview);

由:

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

现在你应该看到两个元素

答案 1 :(得分:0)

modified_string = product_name.replace(menufacture_name + " - ", '')

print("Product name : " + modified_string)