Android listView 绘制表格实例详解
程序员文章站
2024-02-24 18:16:34
android listview 绘制表格
效果图:
二,创建步骤:
1,创建布局:
activity_main中的布局:
<...
android listview 绘制表格
效果图:
二,创建步骤:
1,创建布局:
activity_main中的布局:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_margin="3dip" android:layout_width="match_parent" android:layout_height="match_parent" > <view android:layout_width="match_parent" android:layout_height="1dip" android:background="#a7a5a5"/> <!--调用外部控件的布局list_item布局。--!> <include layout="@layout/list_item" android:id="@+id/table_title"/> <view android:layout_width="match_parent" android:layout_height="1.5dip" android:background="#686867"/> <listview android:id="@+id/list" android:divider="#b4b3b3" android:dividerheight="1.0dip" android:scrollbars="none" android:background="@drawable/listview_bg" android:cachecolorhint="@android:color/transparent" android:fadingedge="none" android:layout_width="fill_parent" android:layout_height="wrap_content" > </listview> </linearlayout>
2,创建list_item布局:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <textview android:id="@+id/text_id" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:paddingbottom="10dip" android:paddingtop="10dip" android:textcolor="#000000" android:textsize="15sp" android:singleline="true" android:text="序号" /> <view android:layout_width="1.5dip" android:layout_height="fill_parent" android:background="#b4b3b3"/> <textview android:id="@+id/text_goods_name" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="3" android:paddingbottom="10dip" android:paddingtop="10dip" android:textcolor="#000000" android:textsize="15sp" android:singleline="true" android:gravity="center" android:text="商品名称" /> <view android:layout_width="1.5dip" android:layout_height="fill_parent" android:background="#b4b3b3"/> <textview android:id="@+id/text_codebar" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="2" android:paddingbottom="10dip" android:textcolor="#000000" android:paddingtop="10dip" android:singleline="true" android:textsize="15sp" android:gravity="center" android:text="条形码" /> <view android:layout_width="1.5dip" android:layout_height="fill_parent" android:background="#b4b3b3"/> <textview android:id="@+id/text_num" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:paddingbottom="10dip" android:paddingtop="10dip" android:textcolor="#000000" android:singleline="true" android:textsize="15sp" android:gravity="center" android:text="数量" /> <view android:layout_width="1.5dip" android:layout_height="fill_parent" android:background="#b4b3b3"/> <textview android:id="@+id/text_curprice" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1.5" android:paddingbottom="10dip" android:textcolor="#000000" android:textsize="15sp" android:singleline="true" android:paddingtop="10dip" android:gravity="center" android:text="现价" /> <view android:layout_width="1.5dip" android:layout_height="fill_parent" android:background="#b4b3b3"/> <textview android:id="@+id/text_money" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1.5" android:paddingbottom="10dip" android:singleline="true" android:textsize="15sp" android:textcolor="#000000" android:paddingtop="10dip" android:gravity="center" android:text="金额" /> </linearlayout>
3,适配器中内容:
tableadapter:
package com.example.listviewtable; import java.util.list; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; public class tableadapter extends baseadapter { private list<goods> list; private layoutinflater inflater; public tableadapter(context context, list<goods> list){ this.list = list; inflater = layoutinflater.from(context); } @override public int getcount() { int ret = 0; if(list!=null){ ret = list.size(); } return ret; } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { goods goods = (goods) this.getitem(position); viewholder viewholder; if(convertview == null){ viewholder = new viewholder(); convertview = inflater.inflate(r.layout.list_item, null); viewholder.goodid = (textview) convertview.findviewbyid(r.id.text_id); viewholder.goodname = (textview) convertview.findviewbyid(r.id.text_goods_name); viewholder.goodcodebar = (textview) convertview.findviewbyid(r.id.text_codebar); viewholder.goodnum = (textview) convertview.findviewbyid(r.id.text_num); viewholder.goodcurrprice = (textview) convertview.findviewbyid(r.id.text_curprice); viewholder.goodmoney = (textview) convertview.findviewbyid(r.id.text_money); convertview.settag(viewholder); }else{ viewholder = (viewholder) convertview.gettag(); } viewholder.goodid.settext(goods.getid()); viewholder.goodid.settextsize(13); viewholder.goodname.settext(goods.getgoodsname()); viewholder.goodname.settextsize(13); viewholder.goodcodebar.settext(goods.getcodebar()); viewholder.goodcodebar.settextsize(13); viewholder.goodnum.settext(goods.getnum()+""); viewholder.goodnum.settextsize(13); viewholder.goodcurrprice.settext(goods.getcurprice()+""); viewholder.goodcurrprice.settextsize(13); viewholder.goodmoney.settext(goods.getmoney()+""); viewholder.goodmoney.settextsize(13); return convertview; } public static class viewholder{ public textview goodid; public textview goodname; public textview goodcodebar; public textview goodnum; public textview goodcurrprice; public textview goodmoney; } }
4,activity中内容:
package com.example.listviewtable; import java.util.arraylist; import java.util.list; import android.app.activity; import android.graphics.color; import android.os.bundle; import android.view.viewgroup; import android.widget.listview; public class listtableactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //设置表格标题的背景颜色 viewgroup tabletitle = (viewgroup) findviewbyid(r.id.table_title); tabletitle.setbackgroundcolor(color.rgb(177, 173, 172)); list<goods> list = new arraylist<goods>(); list.add(new goods("01", "伊利婴儿加盖奶粉110ml", "982323423232",34,23,23)); list.add(new goods("02", "鱼翅", "31312323223",34,23,23)); list.add(new goods("03", "农夫山泉", "12",34,23,23)); list.add(new goods("04", "飞天茅台0", "12333435445",34,23,23)); list.add(new goods("05", "农家小菜", "34523",34,23,23)); list.add(new goods("06", "飞天消费菜", "345456",34,23,23)); list.add(new goods("07", "旺仔小牛奶", "2344",34,23,23)); list.add(new goods("08", "旺旺", "23445",34,23,23)); list.add(new goods("09", "达利园超时牛奶", "3234345",34,23,23)); listview tablelistview = (listview) findviewbyid(r.id.list); tableadapter adapter = new tableadapter(this, list); tablelistview.setadapter(adapter); } }
5,实体类:
商品信息内容:
package com.example.listviewtable; public class goods { private string id; private string goodsname; private string codebar; private float num; private float curprice; private float money; public goods() { super(); } public goods(string id, string goodsname, string codebar, float num, float curprice, float money) { super(); this.id = id; this.goodsname = goodsname; this.codebar = codebar; this.num = num; this.curprice = curprice; this.money = money; } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getgoodsname() { return goodsname; } public void setgoodsname(string goodsname) { this.goodsname = goodsname; } public string getcodebar() { return codebar; } public void setcodebar(string codebar) { this.codebar = codebar; } public float getnum() { return num; } public void setnum(float num) { this.num = num; } public float getcurprice() { return curprice; } public void setcurprice(float curprice) { this.curprice = curprice; } public float getmoney() { return money; } public void setmoney(float money) { this.money = money; } }
demo地址:http://xiazai.jb51.net/201701/yuanma/listviewtable-master(jb51.net).rar
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!