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

2024-05-16 10:48

本文主要是介绍android动态生成表格,使用的是TABLELAYOUT,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


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


android layout button tools table class

使用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. 效果图如下:  


这篇关于android动态生成表格,使用的是TABLELAYOUT的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994696

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加