android动态生成表格,使用的是TABLELAYOUT

来源:互联网 发布:刑法修正案九网络犯罪 编辑:程序博客网 时间:2024/04/29 04:58

使用tablelayout及tablerow生成表格,这里我只生成了一次,可以根据需求更改哦....,对于里面的控件是可以监听的....

mainactivity代码如下:

[html] view plain copy print?
  1. package com.xy.tablerow;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.graphics.Color;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.ImageView;  
  14. import android.widget.TableLayout;  
  15. import android.widget.TableRow;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18. import android.support.v4.app.NavUtils;  
  19.   
  20. public class MainActivity extends Activity implements OnClickListener {  
  21.     Button bu;  
  22.   
  23.     boolean mFlag = false;  
  24.   
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.   
  30.         Button mButton = (Button) findViewById(R.id.myButton);  
  31.         mButton.setOnClickListener(this);  
  32.         bu = new Button(MainActivity.this);  
  33.         bu.setText("tianjia");  
  34.         bu.setId(0x10);  
  35.         bu.setOnClickListener(this);  
  36.     }  
  37.   
  38.     public void addWegit() {  
  39.         TableLayout table = (TableLayout) findViewById(R.id.tablelayout);  
  40.         table.setStretchAllColumns(true);  
  41.         for (int i = 0; i < 6; i++) {  
  42.             TableRow tablerow = new TableRow(MainActivity.this);  
  43.             tablerow.setBackgroundColor(Color.rgb(222, 220, 210));  
  44.             for (int j = 0; j < 3; j++) {  
  45.   
  46.                 if (i == 0 && j == 0) {  
  47.   
  48.                     tablerow.addView(bu);  
  49.                 } else {  
  50.                     EditText testview = new EditText(MainActivity.this);  
  51.                     testview.setBackgroundResource(R.drawable.shape);  
  52.                     // testview.setText("选择");  
  53.                     tablerow.addView(testview);  
  54.                 }  
  55.   
  56.             }  
  57.             table.addView(tablerow, new TableLayout.LayoutParams(  
  58.                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  59.         }  
  60.     }  
  61.   
  62.     @Override  
  63.     public void onClick(View v) {  
  64.   
  65.         int vv = v.getId();  
  66.         switch (vv) {  
  67.         case 0x10:  
  68.             Toast.makeText(MainActivity.this, "heh,keyidianjide",  
  69.                     Toast.LENGTH_SHORT).show();  
  70.             break;  
  71.         case R.id.myButton:  
  72.             if (!mFlag) {  
  73.                 addWegit();  
  74.                 mFlag = !mFlag;  
  75.             }  
  76.   
  77.             break;  
  78.         }  
  79.   
  80.     }  
  81. }  

xml布局如下:
[html] view plain copy print?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/myButton"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="动态生成表格" />  
  12.   
  13.     <TableLayout  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:background="#dedcd2"  
  17.         android:stretchColumns="*" >  
  18.   
  19.         <TableRow  
  20.             android:layout_margin="0.5dip"  
  21.             android:background="#dedcd2" >  
  22.   
  23.             <TextView  
  24.                 android:background="#ffffff"  
  25.                 android:gravity="center"  
  26.                 android:text="年度"  
  27.                 android:textSize="20dip"  
  28.                 android:textStyle="bold" />  
  29.   
  30.             <TextView  
  31.                 android:gravity="center"  
  32.                 android:background="#ffffff"  
  33.                 android:text="本金"  
  34.                 android:textSize="20dip"  
  35.                 android:textStyle="bold" />  
  36.   
  37.             <TextView  
  38.                 android:gravity="center"  
  39.                 android:background="#ffffff"  
  40.                 android:text="利息"  
  41.                 android:textSize="20dip"  
  42.                 android:textStyle="bold" />  
  43.         </TableRow>  
  44.     </TableLayout>  
  45.   
  46.     <TableLayout  
  47.         android:id="@+id/tablelayout"  
  48.         android:layout_width="wrap_content"  
  49.         android:layout_height="wrap_content"  
  50.         android:background="#dedcd2"  
  51.         android:stretchColumns="*" >  
  52.   
  53.         <TableRow  
  54.             android:layout_margin="0.5dip"  
  55.             android:background="#dedcd2" >  
  56.   
  57.             <TextView  
  58.                 android:layout_margin="1dip"  
  59.                 android:background="#ffffff"  
  60.                 android:text=""  
  61.                 android:textSize="20dip"  
  62.                 android:textStyle="bold" />  
  63.   
  64.             <TextView  
  65.                 android:layout_margin="1dip"  
  66.                 android:background="#ffffff"  
  67.                 android:text=""  
  68.                 android:textSize="20dip"  
  69.                 android:textStyle="bold" />  
  70.   
  71.             <TextView  
  72.                 android:layout_margin="1dip"  
  73.                 android:background="#ffffff"  
  74.                 android:text=""  
  75.                 android:textSize="20dip"  
  76.                 android:textStyle="bold" />  
  77.         </TableRow>  
  78.     </TableLayout>  
  79.   
  80. </LinearLayout>  
  81.   
  82. 效果图如下:  


1
1
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两岁半宝宝说话结巴怎么办 孩子咬字不清怎么办 夜里睡觉腿抽筋怎么办 宝宝脾气很不好怎么办 4岁不讲话怎么办 宝宝就是不说话怎么办 说话伤害了朋友怎么办 小孩不开口说话怎么办 左胸口比右胸高怎么办 小孩动不动就生气怎么办 小孩生气手抽筋怎么办 孩子玩电脑游戏成瘾怎么办 加减法孩子学不会怎么办 孩子学习学不会怎么办 脸部五官太小怎么办 小班教案我该怎么办 鼠标不能拖拽怎么办 病了怎么办教学反思 想开水果店没经验怎么办 婴儿听力筛查未通过怎么办 怀孕吐到胃出血怎么办 hp打印机颜色浅怎么办 爱普生打印机没有红色怎么办 孕吐吐到胃出血怎么办 孩子不会写2怎么办 口渴了怎么办大班教案 小班健康跌倒了怎么办 刷油漆有纹路怎么办 纯白门会发黄怎么办 发现小孩子偷钱怎么办 青春期孩子偷钱怎么办 孩子偷钱屡教不改怎么办 孩子经常偷钱怎么办 小孩子总是偷钱怎么办 小孩喜欢偷钱怎么办 小孩老是偷钱怎么办 如果孩子偷钱怎么办 小孩在家偷钱怎么办 买了西晒房怎么办 乳胶漆有打磨痕迹怎么办 墙上乳胶漆用水擦完太亮了怎么办