How to add custom view to the layout?

后端 未结 7 1727
灰色年华
灰色年华 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条回答
  •  旧时难觅i
    2020-11-30 08:21

    i finaly got it here is the code the XML code

    
    

    and the class

    package com.customfonts.namespace;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.Path.Direction;
    import android.util.AttributeSet;
    import android.view.View;
    
    
    
    public class BreakDownBar extends View {
    
        public BreakDownBar(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
            @Override
            protected void onDraw(Canvas canvas) {
                    //Draw rectangle;
                    Path rect = new  Path();
                    rect.addRect(0, 0,250, 150,Direction.CW);
                    Paint cpaint = new Paint();
                    cpaint.setColor(Color.GREEN); 
                    canvas.drawPath(rect, cpaint);
            }
    }
    

提交回复
热议问题