2013-05-01 41 views
1

我在绘制应用程序,其中已经有一个按钮。但我想添加另一个按钮。当我这样做时,它会覆盖上一个按钮。在Android中以编程方式在布局中添加更多视图

我有点新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); 
} 

我的MainActivity:

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

如果你想要多个视图,那么你需要使用一个布局来包装它们。在你的情况下,你可以使用FrameLayout而不是View来继承。直接在那里添加按钮。

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()多次刚刚替换它。

+0

嗨我试过这个,但仍然重叠第一个按钮 – 2013-05-01 17:17:20

+0

如果你不想让它们重叠,那么你可以使用LinearLayout。首先添加您的自定义视图,然后添加按钮。有关布局的更多信息:http://developer.android.com/guide/topics/ui/declaring-layout.html – SimonSays 2013-05-01 17:33:16

+0

嗨simosays,我将它更改为LinearLayout,它不再重叠,但现在的问题是我不能在画布上画画。怎么了? – 2013-05-03 13:58:03

相关问题