Android自定义View - 为 View 添加边框

2017-01-02  本文已影响446人  谷鸽不爱吃稻谷

一、环境

  1. 安卓系统:4.2
  2. 操作系统:Win 8.1
  3. 工具:Android Studio

二、添加边框

public class MyRelativeLayout extends RelativeLayout {    
        public MyRelativeLayout(Context context) {        
            super(context);    
        }    

        public MyRelativeLayout(Context context, AttributeSet attrs) {        
            super(context, attrs);    
        } 

        public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {        
            super(context, attrs, defStyleAttr);    
        }    

        @Override    
        protected void onDraw(Canvas canvas) {        
            Paint paint = new Paint();
            paint.setColor(0xFF000000);     //画笔颜色        
            paint.setStrokeWidth(5);        //画笔粗细
            int width = this.getWidth();        
            int height = this.getHeight();
            //drawLine 参数为坐标(X开始,Y开始,X结束,Y结束,画笔)
            canvas.drawLine(1, height-1, width-1, height-1, paint);        
            super.onDraw(canvas);    
        }
}
上一篇 下一篇

猜你喜欢

热点阅读