0
我有一個擴展視圖的類B,我從A類調用它,它通過setContentView在類B中擴展了活動。我可以使用鼠標移動監聽器和繪畫函數繪製任何東西。現在我想在我的B類上添加按鈕,以便我可以去A類並做一些修改。 我嘗試了一些代碼,但沒有奏效。如何將一個按鈕添加到在Android中擴展視圖的類?
public class MotionDraw extends View {
private Paint paint = new Paint();
private Path path = new Path();
//LinearLayout ll=new LinearLayout(getContext());
public MotionDraw(Context context, AttributeSet attrs,int x) {
super(context, attrs);
//this.setOrientation(LinearLayout.VERTICAL);
//Button b=new Button(getContext());
//b.setText("Back"); // better: getContext().getString(R.string.someString);
//this.addView(b);
paint.setAntiAlias(true);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
//paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.MITER);
if(x == 1)
paint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
some code ...
}
// Schedules a repaint.
invalidate();
return true;
}
}
當我將它擴展到RelativeLayout時,我無法在屏幕或畫布上繪製任何東西。 – user1407990