【问题标题】:Can't override onMeasure method无法覆盖 onMeasure 方法
【发布时间】:2011-02-01 16:39:55
【问题描述】:

我需要重写 onMeasure 方法,但是在尝试重写时出现错误: The method OnMeasure(int, int) of type DrawLnClass.DrawLn must override a superclass method

这是我的代码:

public class DrawLnClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(new DrawLn(this));
    }
    public class DrawLn extends View {

        Paint _paint;
        int _height;
        int _width;
        Bitmap _bitmap;
        Canvas _canvas;
        Point[] xLine;
        Point[] yLine;

        public DrawLn(Context context) {
            super(context);
            _paint = new Paint();
            _paint.setColor(Color.CYAN);
            _paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            _width = View.MeasureSpec.getSize(widthMeasureSpec);
            _height = View.MeasureSpec.getSize(heightMeasureSpec);

            Toast.makeText(getContext(), "Width = " + _width, Toast.LENGTH_SHORT).show();
            Toast.makeText(getContext(), "Height = " + _height, Toast.LENGTH_SHORT).show();

            setMeasuredDimension(_width, _height);  

            //_bitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
            //_canvas = new Canvas(_bitmap);

            //calcLinePlacements();
            //drawBoard();

        }

【问题讨论】:

    标签: android custom-view


    【解决方案1】:

    第一个 O 大写,应该是 onMeasure 而不是 OnMeasure。

    我打赌你很高兴你使用了@Override,否则你不会意识到这个方法没有正确覆盖。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 2016-12-15
      相关资源
      最近更新 更多