前两天在些项目的时候碰到经常使用的GridView要实现一些分割线,以前就是用本方法利用listView和Item的背景颜色的不一样线显示分割线。这是最low的一种作法。因而我就简单的写了一个自定义的 GridView。css
android:divider android:dividerHeight
然而咱们都知道 GirdView默认是没有网格线的,那么该如何设置呢?java
<GridView android:id="@+id/mgv_griview2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:background="@color/black3" android:horizontalSpacing="1dp" android:verticalSpacing="1dp" android:padding="2dp" android:numColumns="3" >
布局android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:gravity="center" android:layout_margin="5dp" android:layout_height="match_parent"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/ic_launcher" android:id="@+id/myitem_gv"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="10dp" android:text="图片"/> </LinearLayout>
效果图 canvas
**
* 类功能描述:</br>
* Created by Administrator on 2017/2/19 0018. * 博客地址: http://blog.csdn.net/androidstarjack * @author androidstar * @version 1.0 </p> 修改时间:</br> 修改备注:</br> * 公众号: 终端研发部 */ public class MyGridView extends GridView { /** * 默认的分割线的颜色 * 也能够在布局中设置 */ private int diverColor = R.color.color1; /** * 默认的分割线的高度 * 也能够在布局中设置 */ private int diverHeight = 1; /** * 所使用的画笔 */ private Paint paint; private Context context; public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); this.context =context; TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.gv_acusttrs); diverHeight = (int) typedArray.getDimension(R.styleable.gv_acusttrs_divierHeight,10); diverColor = typedArray.getResourceId(R.styleable.gv_acusttrs_divierColor,R.drawable.editext_slelect_black4); typedArray.recycle(); paint = new Paint(); paint.setColor(ContextCompat.getColor(context,diverColor)); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); paint.setStrokeWidth(diverHeight); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } /** * 动态修改默认的分割线的颜色 */ public void setDiverColor(int diverColor){ this.diverColor = diverColor; invalidate(); } /** * 动态修改默认的分割线的颜色 */ public void setDiverHeight(int diverHeight){ this.diverHeight = diverHeight; invalidate(); } /** * * @param canvas */ /* @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rect = new Rect(); rect.left = DensityUtil.getScreenIntWidth(context) / 4; rect.top = DensityUtil.getScreenIntHeight(context) / 4; rect.right = DensityUtil.getScreenIntWidth(context)/ 4 * 3; rect.bottom = DensityUtil.getScreenIntHeight(context)/ 4 * 3; canvas.drawRect(rect,paint); }*/ @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); View localView1 = getChildAt(0);//获得第一个view int column = getWidth() / localView1.getWidth();//列数 int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View cellView = getChildAt(i); if ((i + 1) % column == 0) {//每一行最后一个 canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), paint); } else if ((i + 1) > (childCount - (childCount % column))) {//最后一行的item canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), paint); } else { canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), paint); canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), paint); } } } }
咱们都知道这里用到了dispatchDraw方法iview
/** * Called by draw to draw the child views. This may be overridden * by derived classes to gain control just before its children are drawn * (but after its own view has been drawn). * @param canvas the canvas on which to draw the view */ protected void dispatchDraw(Canvas canvas) { }
相关demo如今地址:
MyGridViewApplication.raride