一、基础知识:

TableLayout置底,TableRow在TableLayout的上面,而Button、TextView等控件就在TableRow之上,

另外,TableLayout之上也可以单独放控件。TableLayout是一个使用复杂的布局,最简单的用法就仅

仅是拖拉控件做出个界面,但实际上,会经常在代码里使用TableLayout,例如做出表格的效果。

Android:collapseColumns:以第0行为序,隐藏指定的列
android:shrinkColumns:以第0行为序,自动延伸指定的列填充可用部分
android:stretchColumns:以第0行为序,尽量把指定的列填充空白部分

二、方案一代码展示:

方案一采用xml布局,在代码中除了显示layout之外,未作任何布局相关的操作。

1."Acticity_06\src\yan\acticity_06\MainActivity.Java"

  1. package yan.activity_06;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. public class MainActivity extends Activity {
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. }
  10. }

2."Activity_06\res\layout\activity_main.xml"

  1. <TableLayout 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. tools:context=".MainActivity"
  6. android:stretchColumns="0" >
  7. <TableRow>
  8. <TextView
  9. android:text="第一行第一列"
  10. android:background="#aa0000"
  11. android:padding="3dip" />
  12. <TextView
  13. android:text="第一行第二列"
  14. android:padding="3dip"
  15. android:gravity="center_horizontal"
  16. android:background="#00aa00"
  17. ></TextView>
  18. <TextView
  19. android:text="第一行第三列"
  20. android:gravity="right"
  21. android:background="#0000aa"
  22. android:padding="3dip" />
  23. </TableRow>
  24. <TableRow>
  25. <TextView
  26. android:text="第二行第一列"
  27. android:padding="3dip" />
  28. <TextView
  29. android:text="第二行第二列"
  30. android:gravity="right"
  31. android:padding="3dip" />
  32. </TableRow>
  33. <TextView
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="@string/hello_world" />
  37. </TableLayout>

三、方案二代码展示:

方案二采用代码布局,在layout文件中除了显示一个空的TabLayout之外,未作任何其它布局。

1."Acticity_06\src\yan\acticity_06\MainActivity.java"

  1. package yan.activity_06;
  2. import android.os.Bundle;
  3. import android.view.ViewGroup;
  4. import android.widget.TableLayout;
  5. import android.widget.TableRow;
  6. import android.widget.TextView;
  7. import android.app.Activity;
  8. public class MainActivity extends Activity {
  9. /** Called when the activity is first created. */
  10. private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
  11. private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. //新建TableLayout01的实例
  17. TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);
  18. //全部列自动填充空白处
  19. tableLayout.setStretchAllColumns(true);
  20. //生成10行,8列的表格
  21. for(int row=0;row<10;row++)
  22. {
  23. TableRow tableRow=new TableRow(this);
  24. for(int col=0;col<8;col++)
  25. {
  26. //tv用于显示
  27. TextView tv=new TextView(this);
  28. tv.setText("("+col+","+row+")");
  29. tableRow.addView(tv);
  30. }
  31. //新建的TableRow添加到TableLayout
  32. tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));
  33. }
  34. }
  35. }

2."Activity_06\res\layout\activity_main.xml"

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TableLayout
  8. android:id="@+id/TableLayout01"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content">
  11. </TableLayout>
  12. </LinearLayout>

三、效果展示:

1.方案一:

2.方案二:

参考文章有: http://blog.csdn.net/hellogv/article/details/4523745

http://blog.csdn.net/hellogv/article/details/4522125

本文完整代码下载地址: http://download.csdn.net/detail/ypist/5144652

本文博客源地址:http://blog.csdn.net/ypist

界面布局之表格布局TableLayout+TableRow的更多相关文章

  1. Android基础_2 Activity线性布局和表格布局

    在activity的布局中,线性布局和表格布局是最简单的,这次分别从线性布局,表格布局以及线性布局和表格混合布局做了实验,实验中只需要编写 相应的xml的代码,java代码不需要更改,因为我们这里只是 ...

  2. WPF的布局-Grid(表格布局)

    1. Grid布局就是表格布局 如下图: 2. 使用方法 2.1. 先生成适量的行和列,代码如下: <Grid><!--使用Grid控件--> <Grid.ColumnD ...

  3. Android布局_表格布局TableLayout

    一.TableLayout概述 TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象 二.TableLayout的全局属性  1 ...

  4. Android:控件布局(表格布局)TableLayout

    TableLayout继承LinearLayout 实例:用表格布局实现计算机布局>>>>>>>>>>>> 有多少个TableR ...

  5. Android——布局(线性布局linearLayout,表格布局TableLayout,帧布局FrameLayout)

    线性布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...

  6. android——相对布局,表格布局

    1.相对布局 RelativeLayout 又称作相对布局,也是一种非常常用的布局.和LinearLayout 的排列规则不同,RelativeLayout 显得更加随意一些,它可以通过相对定位的方式 ...

  7. Android -- UI布局管理,相对布局,线性布局,表格布局,绝对布局,帧布局

    1. 相对布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  8. Android——excise(用线性布局、表格布局、相对布局做发送邮件界面)

    LinearLayout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  9. Android笔记(十) Android中的布局——表格布局

    TableLayout运行我们使用表格的方式来排列控件,它的本质依然是线性布局.表格布局采用行.列的形式来管理控件,TableLayout并不需要明确的声明包含多少行多少列,而是通过添加TableRo ...

随机推荐

  1. doPost()和doGet()

    GET 请求的一些特点: GET 请求会有 cache GET 请求会保留在浏览历史中 GET 请求可以保存到书签 GET 请求不应用于处理敏感数据 GET 请求有长度限制 GET 请求应该只用于获取 ...

  2. [Spring常见问题]java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    这个问题是因为部署在tomcat下的项目中没有springweb包 但是问题来了,但是我的项目中有呀,maven都引了呀,然后我就懵B啦!看到这个博客我就豁然开朗了:http://my.oschina ...

  3. 在access中如何创建数据库。你认为数据库在网站开发中所扮演的角色是什么。使用数据库和使用文件,两者的优缺点各是什么。

    首先在access里面填写所用的信息,将数据库创建,在导入程序设计里进行完成代码. 首先打开我们的access程序,打开方法是单击开始——所有程序. 所有程序中找到microsoft office文件 ...

  4. 数据库助手类 DBHelper

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  5. 读书笔记 --TCP :传输控制协议(一)

    TCP提供一种面向连接的,可靠的字节流服务. TCP 通过如下方式来提供可靠性: 应用数据被分割成TCP认为最适合发送的数据块. 超时重传机制.TCP发出一个段后,启动一个定时器,等待目的端确认收到这 ...

  6. Hibernate Spring

    原理: 1.         读取并解析配置文件 2.         读取并解析映射信息,创建SessionFactory 3.         打开Sesssion 4.         创建事务 ...

  7. C# : CEF操作

    代码挺差的,仅供学习.参考 class CEFGlueLoader { class CefAppImpl : CefApp { protected override void OnBeforeComm ...

  8. PostgreSQL的OGG -- bucardo

    bucardo是PostgreSQL数据库中实现双向同步的软件,可以实现PostgreSQL数据库的双master的方案,不过bucardo中的同步都是异步的,它是通过触发器记录变化,程序是perl写 ...

  9. BeanUtils.copyProperties()方法和PropertyUtils.copyProperties()的区别

    首先两者来源于同一个包: import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.Prop ...

  10. Prince2七大主题之风险

    Prince2七大主题之风险   我们前几个节学习了PRINCE2七大主题的商业论证.组织.质量和计划,今天我们开展对于风险模块的讲解. 风险:目的是识别.评估和控制不确定性,从而提高项目的成功率.P ...