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

2017-03-08  本文已影响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);
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读