如何给gridview的单元格加上分割线
答案:2 悬赏:30 手机版
解决时间 2021-02-07 20:42
- 提问者网友:美人性情
- 2021-02-07 01:03
如何给gridview的单元格加上分割线
最佳答案
- 五星知识达人网友:拾荒鲤
- 2021-02-07 01:38
重载dispatchDraw
public class LineGridView extends GridView{
public LineGridView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public LineGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LineGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void dispatchDraw(Canvas canvas){
super.dispatchDraw(canvas);
View localView1 = getChildAt(0);
int column = getWidth() / localView1.getWidth();
int childCount = getChildCount();
Paint localPaint;
localPaint = new Paint();
localPaint.setStyle(Paint.Style.STROKE);
localPaint.setColor(getContext().getResources().getColor(R.color.grid_line));
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(), localPaint);
}else if((i + 1) > (childCount - (childCount % column))){
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
}else{
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
}
}
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, lastView.getBottom(), localPaint);
}
}
}
}
注意: 最边上的格子需要特殊处理。super.dispatchDraw(canvas);一定要调用,不然格子中的内容(子view)就得不到绘制的机会!
public class LineGridView extends GridView{
public LineGridView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public LineGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LineGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void dispatchDraw(Canvas canvas){
super.dispatchDraw(canvas);
View localView1 = getChildAt(0);
int column = getWidth() / localView1.getWidth();
int childCount = getChildCount();
Paint localPaint;
localPaint = new Paint();
localPaint.setStyle(Paint.Style.STROKE);
localPaint.setColor(getContext().getResources().getColor(R.color.grid_line));
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(), localPaint);
}else if((i + 1) > (childCount - (childCount % column))){
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
}else{
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
}
}
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, lastView.getBottom(), localPaint);
}
}
}
}
注意: 最边上的格子需要特殊处理。super.dispatchDraw(canvas);一定要调用,不然格子中的内容(子view)就得不到绘制的机会!
全部回答
- 1楼网友:神鬼未生
- 2021-02-07 02:33
重载dispatchdraw public class linegridview extends gridview{ public linegridview(context context) { super(context); // todo auto-generated constructor stub } public linegridview(context context, attributeset attrs) { super(context, attrs); } public linegridview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override protected void dispatchdraw(canvas canvas){ super.dispatchdraw(canvas); view localview1 = getchildat(0); int column = getwidth() / localview1.getwidth(); int childcount = getchildcount(); paint localpaint; localpaint = new paint(); localpaint.setstyle(paint.style.stroke); localpaint.setcolor(getcontext().getresources().getcolor(r.color.grid_line)); 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(), localpaint); }else if((i + 1) > (childcount - (childcount % column))){ canvas.drawline(cellview.getright(), cellview.gettop(), cellview.getright(), cellview.getbottom(), localpaint); }else{ canvas.drawline(cellview.getright(), cellview.gettop(), cellview.getright(), cellview.getbottom(), localpaint); canvas.drawline(cellview.getleft(), cellview.getbottom(), cellview.getright(), cellview.getbottom(), localpaint); } } 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, lastview.getbottom(), localpaint); } } }} 注意: 最边上的格子需要特殊处理。super.dispatchdraw(canvas);一定要调用,不然格子中的内容(子view)就得不到绘制的机会!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯