如何在Android中扩展View的类添加按钮?

时间:2012-05-25 11:12:53

标签: android android-layout android-view

我有一个扩展View的类B,我从A类调用它,它通过setContentView.In扩展Activity。我可以使用鼠标移动侦听器和绘制函数绘制任何东西。现在我想在我的B级添加按钮,这样我就可以去A级并做一些修改。 我尝试了一些代码但是没有用。

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

}

1 个答案:

答案 0 :(得分:0)

对于MotionDraw,尝试扩展RelativeLayout而不是View。

相关问题