美文网首页
给GridView的单元格添加一个半封闭的边框

给GridView的单元格添加一个半封闭的边框

作者: 留给时光吧 | 来源:发表于2017-03-08 16:31 被阅读0次

    可以看出图中的单元格有一个半封闭的边框,这里我们可以重写gridview的dispatchDraw来实现:

    @Override
        protected void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
            int column = getNumColumns(); //获取列数
            int row = (int) Math.ceil(getChildCount() / (float)column); //获取行数
            int childCount = getChildCount();
            Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            linePaint.setStyle(Paint.Style.STROKE);
            linePaint.setColor(Color.WHITE);
            for (int i = 0; i<childCount ; i++){
                View child = getChildAt(i);
                if((i+1) > column * (row - 1)){
                    if((i + 1) == column * row){
                        continue; 
                    }else{
                        canvas.drawLine(child.getRight(),child.getTop(),child.getRight()
                                        ,child.getBottom(),linePaint);
                    }
                }else if ((i+1) % column ==0){
                   canvas.drawLine(child.getLeft(),child.getBottom(),child.getRight()
                                     ,child.getBottom(),linePaint);
                }else{
                    canvas.drawLine(child.getLeft(),child.getBottom(),child.getRight()
                                     ,child.getBottom(),linePaint);
                    canvas.drawLine(child.getRight(),child.getTop(),child.getRight()
                                     ,child.getBottom(),linePaint);
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:给GridView的单元格添加一个半封闭的边框

          本文链接:https://www.haomeiwen.com/subject/qmwbgttx.html