表格的动态添加
来源:互联网 发布:杭州linux运维工资 编辑:程序博客网 时间:2024/05/17 00:08
开发中有时候要用到表格的形式,而且是不规则的表格如上图,根据开发中拉取到的数据来创建表格,该表格原理是TableLayout 添加 TableRow和在TableRow里添加TableLayout 互相添加来实现的,不过有个问题就是当数据量比较大的时候会加载比较慢,我代码里是让表格显示出来后再让加载的dialog消失的,但是在手机上显示的情况确实,dialog消失后再过5到6秒表格才显示出来下面直接上代码
Gson gson = new Gson(); fenJieBean = gson.fromJson(result, FenJieBean.class);//result为请求到的数据,fenJieBean为存储数据的对象 String rileTitle=fenJieBean.fileTitle; title.setText(rileTitle); //显示表格信息 displayRegeditedInfo(); mDialog.dismiss();
下面是表格设置方法
private void displayRegeditedInfo() { // TODO Auto-generated method stub //(TableRow)子控件的宽高1 layoutParam1= new TableRow.LayoutParams(0,LayoutParams.MATCH_PARENT,1.0f) ;//权重设置表格宽度 //(TableRow)的子控件的宽高2 layoutParam2= new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT,2.0f); //(TableRow)的子控件的宽高3 layoutParam3= new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT,3.0f); //(TableRow)的子控件的宽高4 layoutParam4= new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT,10.0f); //(TableRow)的子控件的宽高5 layoutParam5= new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT,9.0f); //先添加表格标题头行,因为表头是固定的,所以直接写死 TableRow tRow = new TableRow(mContext); tRow.setOrientation(TableRow.HORIZONTAL); TextView b1 = textMethod1("序号"); tRow.addView(b1); TextView b2 = textMethod1("目标任务名称"); tRow.addView(b2); TextView b3 = textMethod3("内容要求"); tRow.addView(b3); TextView b4 = textMethod2("推进计划"); tRow.addView(b4); TextView b5 = textMethod1("完成时限"); tRow.addView(b5); TextView b6 = textMethod1("责任处室"); tRow.addView(b6); TextView b7 = textMethod1("责任人"); tRow.addView(b7); TextView b11 = textMethod1("备注"); tRow.addView(b11); tableLayout.addView(tRow);//tableLayout为xml布局里的TableLayout对象,这行代码代表把表格第一行标题行添加上了 for(int i=0;i<fenJieBean.list.size();i++){ FenJieContent fenJieContent = fenJieBean.list.get(i);//得到行的对象 //新建一行显示每个成员的具体信息 TableRow personRow = new TableRow(mContext);//personRow表示一行的对象 personRow.setOrientation(TableRow.HORIZONTAL); String xuhao=fenJieContent.XUHAO; TextView t = textMethod1( xuhao); List<RenWuContent> renwuList=fenJieContent.renwuList; TableLayout tableLayout1 = new TableLayout(mContext); tableLayout1.setLayoutParams(layoutParam4); for(int k=0;k<renwuList.size();k++){ //建行对象 TableRow personRow1 = new TableRow(mContext);//personRow表示一行的对象 personRow1.setOrientation(TableRow.HORIZONTAL); RenWuContent renWuContent = renwuList.get(k); String renwu=renWuContent.RENWU; TextView t1 = textMethod1( renwu); TableLayout tableLayout2 = new TableLayout(mContext); tableLayout2.setLayoutParams(layoutParam5); for(int w=0;w<renWuContent.jdList.size();w++){ JdContent jdContent = renWuContent.jdList.get(w);//建行对象 TableRow personRow2 = new TableRow(mContext);//personRow表示一行的对象 personRow2.setOrientation(TableRow.HORIZONTAL); String yaoqiu=jdContent.YAOQIU; TextView t2 = textMethod3(yaoqiu); personRow2.addView(t2); if(jdContent.JIHUA !=null&&(!"".equals(jdContent.JIHUA))){ String jihua=jdContent.JIHUA; TextView t3 = textMethod2(jihua); personRow2.addView(t3); }else{ personRow2.addView(textMethod2("")); } if(jdContent.SHIXIAN !=null&&(!"".equals(jdContent.SHIXIAN))){ String shixian=jdContent.SHIXIAN; TextView t4 = textMethod1(shixian); personRow2.addView(t4); }else{ personRow2.addView(textMethod1("")); } if(jdContent.CHUSHI !=null&&(!"".equals(jdContent.CHUSHI))){ String chushi=jdContent.CHUSHI; TextView t5 = textMethod1(chushi); personRow2.addView(t5); }else{ personRow2.addView(textMethod1("")); } if(jdContent.ZERENREN !=null&&(!"".equals(jdContent.ZERENREN))){ String zerenren=jdContent.ZERENREN; TextView t6 = textMethod1(zerenren); personRow2.addView(t6); }else{ personRow2.addView(textMethod1("")); } if(jdContent.REMARK !=null&&(!"".equals(jdContent.REMARK))){ String remark=jdContent.REMARK; TextView t7 = textMethod1(remark); personRow2.addView(t7); }else{ personRow2.addView(textMethod1("")); } tableLayout2.addView(personRow2); } personRow1.addView(t1);// personRow1.addView(tableLayout2); tableLayout1.addView(personRow1); } personRow.addView(t);// personRow.addView(tableLayout1); tableLayout.addView(personRow); } }
设置textView对象的背景,以及属性,
private TextView textMethod1(String s) { // TODO Auto-generated method stub TextView textView = new TextView(mContext); textView.setBackgroundResource(R.drawable.gradient_box); textView.setLayoutParams(layoutParam1); textView.setTextSize(16); textView.setGravity(Gravity.CENTER); String value = s.replaceAll(" ", "\n"); value = s.replaceAll("  ", ""); textView.setText(value); textView.setTextSize(16); textView.setPadding(1, 10, 1, 10); return textView; } private TextView textMethod2(String s) { // TODO Auto-generated method stub TextView textView = new TextView(mContext); textView.setBackgroundResource(R.drawable.gradient_box);//之所以看到现在的表格,其实就是给每个textView对象添加了这个边框 textView.setLayoutParams(layoutParam2); textView.setTextSize(16); textView.setGravity(Gravity.CENTER); String value = s.replaceAll(" ", "\n"); value = s.replaceAll("  ", ""); textView.setText(value); textView.setTextSize(16); textView.setPadding(1, 10, 1, 10); return textView; } private TextView textMethod3(String s) { // TODO Auto-generated method stub TextView textView = new TextView(mContext); textView.setBackgroundResource(R.drawable.gradient_box); textView.setLayoutParams(layoutParam3); textView.setTextSize(16); textView.setGravity(Gravity.CENTER); String value = s.replaceAll(" ", "\n"); value = s.replaceAll("  ", ""); textView.setText(value); textView.setTextSize(16); textView.setPadding(1, 10, 1, 10); return textView; }
gradient_box.xml文件
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/xian"></solid> <stroke android:width="0.25dp" android:color="#3000" /></shape>
阅读全文
0 0
- 表格的动态添加
- 动态添加表格的行和列
- js实现表格行的动态添加
- js实现表格的动态添加
- js实现动态添加表格的行
- jquary实现的动态添加表格效果
- JS 动态添加、删除表格的行
- 动态添加表格行
- 动态添加表格
- 动态添加表格行
- 动态添加表格行
- 表格动态添加删除
- javascript动态添加表格
- 动态添加删除表格
- js动态添加表格
- jquery动态添加表格
- javascript 动态添加表格
- js动态添加表格
- Java wait() 和 notify() 理解
- 做好这些细节就能让你的网站上首页?
- 全功能的100米KVM延长器
- HTTP协议详解
- Android Scroller解析和使用
- 表格的动态添加
- 使用LANMPS 一键PHP环境安装包在1G内存下报g++: internal compiler error: Killed (program cc1plus)
- 第一个博客
- b#^ll762w37564231.;l-joj13@$@#
- 毕设-创建SpringBoot项目
- 亿级Web系统搭建——单机到分布式集群
- Python 添加图例出错:UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x07FA3210>]
- IT 行业与风险投资
- 为爱而码