今天我们学习了如何更好的利用Android 的 layout 布局。

接下来是个简单的栗子去了解这个自动生成的动态的控件(自动生成表格)

这是我们的layout 页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入要生成表格的行号列"
android:textSize="15sp"/>
<TableRow
android:gravity="center"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="行"
android:textSize="15sp"/>
<EditText
android:id="@+id/et1"
android:layout_width="100dp"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:gravity="center"
android:layout_marginTop="5dp"
> <TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="列"
android:textSize="15sp" /> <EditText
android:id="@+id/et2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:numeric="decimal"/>
<!-- decimal 只能输入数字,大于0的数字-->
</TableRow>
<TableRow
android:gravity="center"
>
<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="确定" />
</TableRow>
</TableLayout>
</LinearLayout>
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:stretchColumns="*"
android:shrinkColumns="*"> </TableLayout>
</LinearLayout>

再看下我们的 mainActivity.class

package com.example.counter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow; public class MainActivity extends Activity implements OnClickListener { private Button submit;
private EditText row;
private EditText cloumn;
private TableLayout table; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.canchange);
submit = (Button) findViewById(R.id.btnadd); row = (EditText) findViewById(R.id.et1);
cloumn = (EditText) findViewById(R.id.et2);
table = (TableLayout) findViewById(R.id.table); submit.setOnClickListener(this); } @Override
public void onClick(View v) {
int c = Integer.parseInt(row.getText() + "");
int r = Integer.parseInt(cloumn.getText() + "");
table.removeAllViews();
for (int i = 0; i < c; i++) {
TableRow tr = new TableRow(this); for (int j = 0; j < r; j++) {
Button b = new Button(this); tr.addView(b);
}
table.addView(tr);
}
} }

运行结果:

可以根据自己的需求去动态生成表格,在这里我们要了解这个动态的概念。

大家可以将代码拷贝去自行实现一番。(调皮~)

android_demo之自动生成动态表格的更多相关文章

  1. ANDROID 自动生成动态表格for

    简单的栗子去了解这个自动生成的动态的控件(自动生成表格) /cs-Layout/res/layout/activity_main.xml <LinearLayout xmlns:android= ...

  2. poi自动生成Ecxel表格和Chart图表

    最近因为业务需求,需要做poi自动导出Ecxel表格和Chart折线图的功能. 所以我在网上找到了一篇关于poi生成Chart图表的博客,代码很详细,但是缺少相关注释说明. 想要将它改造成自己需要的样 ...

  3. 自己写的自动生成动态边框的jquery小插件

    思路就是在元素四周添加<ul>列表,然后周期性地改变它的颜色,实现动态的效果,不支持ie7.ie8 预览链接http://gorey.sinaapp.com/myBorder/border ...

  4. jmeter自动生成报告

    从JMeter 3.0开始已支持自动生成动态报告,我们可以更容易根据生成的报告来完成我们的性能测试报告. 如何生成html测试报告 如果未生成结果文件(.jtl),可运行如下命令生成报告: jmete ...

  5. Jmeter系列-自动生成html报告

    从JMeter 3.0开始已支持自动生成动态报告,我们可以更容易根据生成的报告来完成我们的性能测试报告. 如何生成html测试报告 如果未生成结果文件(.jtl),可运行如下命令生成报告: jmete ...

  6. 自动把动态的jsp页面(或静态html)生成PDF文档,并且上传至服务器

    置顶2017年11月06日 14:41:04 阅读数:2311 这几天,任务中有一个难点是把一个打印页面自动给生成PDF文档,并且上传至服务器,然而公司框架只有手动上传文档,打印时可以保存为PDF在本 ...

  7. Java利用poi生成word(包含插入图片,动态表格,行合并)

    转(小改): Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插 ...

  8. Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成)

    Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成) 动态表单生成 ElementUI官网引导 Element表单生成 Element动态增减表单,在线代码 关键配置 templa ...

  9. java web(七): mybatis的动态sql和mybatis generator自动生成pojo类和映射文件

    前言: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据 不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还 ...

随机推荐

  1. Could not find com.android.tools.build:gradle:1.3.0.

    * What went wrong:          A problem occurred configuring project ':TZYJ_Android'.> Could not re ...

  2. C# 进程间通信之二传递复杂数据类型(转)

    从C#下使用WM_COPYDATA传输数据说到Marshal的应用 笔者曾在一个项目的实施过程中,需要使用WM_COPYDATA在本地机器的两个进程间传输数据.在C++中实现非常简单,但在C#中实现时 ...

  3. java @ResponseBody返回值中去掉NULL字段

    需要同时添加两个位置: 1.annotation-driven过滤 <mvc:annotation-driven> <mvc:message-converters register- ...

  4. Intent传递List和Object和List<Object>

    一.传递List 传递List<String>的方法 小技巧,List<object> 可以使用json 转为 List<string>,就可以使用 List< ...

  5. SQL2008 清除日志

    USE [master] GO ALTER DATABASE BizTest SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE BizTest SE ...

  6. 转:C# 中 MSCHART 饼状图显示百分比

    转自:http://blog.sina.com.cn/s/blog_51beaf0e0100yffo.html 1)显示百分比 Chart1.Series["Series1"].L ...

  7. DES对称性加密

    using System; using System.Security.Principal; using System.Security.Permissions; using System.Secur ...

  8. Linux学习三:Ubuntu下使用minicom和开发板通信

    备注:如果你是用的是Windows则使用超级终端即可:开始-程序-附件-通讯-超级终端 现在我们在Ubuntu下安装配置minicom: 1.进入ubuntu桌面ctrl+alt+t打开终端 输入:s ...

  9. 关于ckeditor ajax提交到后台 问题

    ckeditor 提交时 如果有带有html时是提交不了的 解决办法就是 你在提交的时候 将ckeditor获取的只编码(encodeURI) 然后在传到后台提交的时候 在解码 就ok了 ckname ...

  10. bzoj1051 [HAOI2006]受欢迎的牛

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4773  Solved: 2541[Submit][Sta ...