先上效果图:
上代码:
package com.integration.utils.ui;
import com.integration.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;
public class MyGridView extends GridView {
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
View localView1 = getChildAt(0);
int column = getWidth() / localView1.getWidth();
int childCount = getChildCount();
Paint localPaint = new Paint();
localPaint.setStyle(Paint.Style.STROKE);
localPaint.setColor(getContext().getResources().getColor(R.color.line));
for (int i = 0; i < childCount; i++) {
View cellView = getChildAt(i);
if ((i + 1) % column == 0) {
// 绘制顶部
canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getRight(), cellView.getTop(),
localPaint);
// 最右侧只画底部边框
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(),
localPaint);
} else if ((i + 1) > (childCount - (childCount % column))) {
// 绘制顶部(此处多行时候没有判断,会重复绘制)
canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getRight(), cellView.getTop(),
localPaint);
// 最下部只画右侧边框
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(),
localPaint);
// 绘制底部
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(),
localPaint);
} else {
// 绘制顶部
canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getRight(), cellView.getTop(),
localPaint);
// 画底部和右侧边框
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(),
localPaint);
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(),
localPaint);
}
} // 如果最后一行不足,则补全缺省item的右侧竖线。
if (childCount % column != 0) {
for (int j = 0; j < (column - childCount % column); j++) {
View lastView = getChildAt(childCount - 1);
// 绘制顶部(此处多行时候没有判断,会重复绘制)
canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getTop(),
lastView.getRight() + lastView.getWidth() * (j + 1), lastView.getTop(), localPaint);
//右侧竖线
canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getTop(),
lastView.getRight() + lastView.getWidth() * j, lastView.getBottom(), localPaint);
// 绘制底部
canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getBottom(),
lastView.getRight() + lastView.getWidth() * (j + 1), lastView.getBottom(), localPaint);
}
}
}
}
<com.integration.utils.ui.MyGridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:horizontalSpacing="0.0dip"
android:listSelector="@null"
android:numColumns="4"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="0.0dip" />