How to add custom view to the layout?

后端 未结 7 1725
灰色年华
灰色年华 2020-11-30 07:35

I have a GraphicsView class that extends from the View class and I want to add this GraphicsView class to the main layout in my projec

7条回答
  •  醉话见心
    2020-11-30 08:13

    This is working for me and add the view in XML with full path and try giving wrapcontent for height and width.

    public class RectangleView extends View {
        public RectangleView(Context context) {
            super(context);
        }
    
        public RectangleView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public RectangleView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setColor(Color.GRAY);
            canvas.drawColor(Color.BLUE);
            canvas.drawRect(10,10,50,50, paint);
        }
    }
    

提交回复
热议问题