在Android中以编程方式在布局中添加更多视图

时间:2013-05-01 16:43:50

标签: android layoutparams

我正在做一个油漆应用程序,我已经有了一个按钮。但我想添加另一个按钮。当我这样做时,它与前一个按钮重叠。

我是LayoutParams的新手,所以我需要你的指导。请检查我正在处理的代码:

  public class MyTouchEventView extends View {

private Paint paint = new Paint();
private Path path = new Path();
private Paint circlePaint = new Paint();
private Path circlePath = new Path();

public Button btnReset;
public Button btnSave;
public LayoutParams params;
public LayoutParams params2;

@SuppressWarnings("deprecation")
public MyTouchEventView(Context context) {
    super(context);

    paint.setAntiAlias(true);
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(15f);

    circlePaint.setAntiAlias(true);
    circlePaint.setColor(Color.BLUE);
    circlePaint.setStyle(Paint.Style.STROKE);
    circlePaint.setStrokeJoin(Paint.Join.MITER);
    circlePaint.setStrokeWidth(4f);


    btnReset = new Button(context);
    btnReset.setText("Clear Screen");
    btnSave = new Button(context);
    btnSave.setText("Save Image");

    params = new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    params2 = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    btnReset.setLayoutParams(params);
            btnSave.setLayoutParams(params);



    btnSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // resets the screen
            path.reset();
            // Calls the onDraw() method
            postInvalidate();
        }
    });

    btnReset.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // resets the screen
            path.reset();
            // Calls the onDraw() method
            postInvalidate();
        }
    });

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawPath(path, paint);
    canvas.drawPath(circlePath, circlePaint);
}

我的主要活动:

  public class DrawingBrush extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyTouchEventView tv = new MyTouchEventView(this);

    setContentView(tv);
    addContentView(tv.btnReset,  tv.params);
            addContentView(tv.btnSave,  tv.params);
    }

伙计们,你能帮我弄清楚我在这里缺少什么吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果您想要多个视图,则需要使用布局来包装它们。在您的情况下,您可以使用FrameLayout而不是View继承。直接在其中添加Button。

public class MyTouchEventView extends FrameLayout {

private Paint paint = new Paint();
private Path path = new Path();
private Paint circlePaint = new Paint();
private Path circlePath = new Path();

public Button btnReset;
public Button btnSave;
public FrameLayout.LayoutParams params;
public FrameLayout.LayoutParams params2;

@SuppressWarnings("deprecation")
public MyTouchEventView(Context context) {
    super(context);

    ...

    btnReset = new Button(context);
    btnReset.setText("Clear Screen");
    ...

    params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    btnReset.setLayoutParams(params);
    addView(btnReset);


    ...
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawPath(path, paint);
    canvas.drawPath(circlePath, circlePaint);
}

顺便说一下:您只能设置一个内容视图,因此多次调用setContentView()只会替换它。