欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现带有边框的ListView和item的方法

程序员文章站 2024-03-03 22:18:16
本文实例讲述了android实现带有边框的listview和item的方法。分享给大家供大家参考,具体如下: 想为listview和item四周添加边框有两种方法: 1...

本文实例讲述了android实现带有边框的listview和item的方法。分享给大家供大家参考,具体如下:

想为listview和item四周添加边框有两种方法:

1.贴一张带有边框效果的背景图

2.自定义draw的方法

第一种方法较第二种方法更耗系统资源,但是用法简单,只需要一张图设置为相应控件的背景即可,而第二种灵活性好些。

这次是实现带有边框的listview和item,为此写个简单demo 学习学习

先看下demo运行效果吧

Android实现带有边框的ListView和item的方法

下面是主要代码,主要是用到canvas.drawline(...)代码简单,我就不啰嗦了

borderlistview.java

package com.borderlistview.manymore13;
import android.content.context;
import android.content.res.resources;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.dashpatheffect;
import android.graphics.paint;
import android.util.attributeset;
import android.view.motionevent;
import android.view.view;
import android.widget.listview;
public class borderlistview extends listview{
  public borderlistview(context context) {
    super(context);
  }
  public borderlistview(context context, attributeset attrs)
  {
    super(context, attrs);
  }
  @override
  protected void ondraw(canvas canvas) {
    float width = getwidth();
    float height= getheight();
    int linewidth = 10; // 线宽十个像素
    int graycolor = color.gray;
    paint mlinepaint = new paint();
    mlinepaint.setcolor(graycolor);
    mlinepaint.setstyle(paint.style.stroke);
    mlinepaint.setantialias(true);
    mlinepaint.setstrokewidth(linewidth);
    // 画四周的边框 注意下面的 linewidth/2 不加的话四周的线可能不一样粗
    canvas.drawline(0f, 0+linewidth/2, width, 0+linewidth/2, mlinepaint);
    canvas.drawline(width-linewidth/2, 0, width-linewidth/2, height, mlinepaint);
    canvas.drawline(width-linewidth/2, height-linewidth/2, 0, height-linewidth/2, mlinepaint);
    canvas.drawline(0+linewidth/2, height, 0+linewidth/2, 0,mlinepaint);
    super.ondraw(canvas);
  }
}

listviewitem.java   listview的item 添加虚线和红线

package com.borderlistview.manymore13;
import com.manymore13.mylistview.r;
import android.content.context;
import android.content.res.resources;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.dashpatheffect;
import android.graphics.paint;
import android.view.layoutinflater;
import android.view.view;
import android.widget.framelayout;
import android.widget.relativelayout;
import android.widget.textview;
 public class listviewitem extends relativelayout{
  private view viewholder;
  private textview tveventname;
  private context c;
  private framelayout leftframe;
  public listviewitem(context context) {
    super(context);
    layoutinflater flater = layoutinflater.from(context);
    viewholder = flater.inflate(r.layout.item, this);
    getviewandsetclick();
    c = context;
  }
  private void getviewandsetclick()
  {
    tveventname = (textview)viewholder.findviewbyid(r.id.eventname);
    leftframe = (framelayout)viewholder.findviewbyid(r.id.frame);
  }
  public void seteventname(string name)
  {
    tveventname.settext(name);
  }
  public void updateview()
  {
    this.postinvalidate();
  }
  @override
  protected void dispatchdraw(canvas canvas) {
    super.dispatchdraw(canvas);
    resources res = getresources();
    int graycolor = color.gray;
    int redcolor = res.getcolor(r.color.red);
    int leftframepos = leftframe.getright();
    paint mlinepaint = new paint();
    mlinepaint.setcolor(redcolor);
    mlinepaint.setstyle(paint.style.stroke);
    mlinepaint.setstrokewidth(2);
    //画两条直线
    canvas.drawline(leftframepos+20, 0f, leftframepos+20, getheight(), mlinepaint);
    canvas.drawline(leftframepos+25, 0f, leftframepos+25, getheight(), mlinepaint);
    // 画虚线
    mlinepaint.setcolor(graycolor);
    dashpatheffect effect = new dashpatheffect(new float[] { 5,5, 5, 5, 5}, 3);
    mlinepaint.setantialias(true);
    mlinepaint.setpatheffect(effect);
    canvas.drawline(0, getheight(), getwidth(), getheight(), mlinepaint);
  }
}

mybaseadaper.java

package com.borderlistview.manymore13;
import java.util.list;
import android.content.context;
import android.os.handler;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.view.windowmanager;
import android.widget.baseadapter;
import android.widget.imageview;
public class mybaseadaper extends baseadapter{
  private list<string> list;
  private context c;
  mybaseadaper(context c, list<string> list)
  {
    this.list = list;
    this.c = c;
  }
  @override
  public int getcount() {
    // todo auto-generated method stub
    return list.size();
  }
  @override
  public object getitem(int i) {
    // todo auto-generated method stub
    return list.get(i);
  }
  @override
  public long getitemid(int i) {
    // todo auto-generated method stub
    return i;
  }
  @override
  public view getview(int i, view view, viewgroup viewgroup) {
    listviewitem itemview = null ;
    if(view == null){
      itemview = new listviewitem(c);
    }else{
      itemview = (listviewitem)view;
    }
    itemview.seteventname(list.get(i));
    return itemview;
  }
}

另外,在写本次demo的时候报了错误,有错就改 caused by: java.lang.nosuchmethodexception:borderlistview(context,attributeset)

在 borderlistview类中加一个构造函数 搞定

public borderlistview(context context, attributeset attrs)
{
super(context, attrs);
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。