2013-05-01 105 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

相關問題