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

相关文章

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

java变量内存中存储的使用方式

《java变量内存中存储的使用方式》:本文主要介绍java变量内存中存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、变量的定义3、 变量的类型4、 变量的作用域5、 内存中的存储方式总结1、介绍在 Java 中,变量是用于存储程序中数据