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

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核