一、基础知识:

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. Leetcode: Concatenated Words

    Given a list of words, please write a program that returns all concatenated words in the given list ...

  2. PeopleSoft Home Subdirectories

    PeopleSoft Home Subdirectories appserv — home to the Application Server and Process Scheduler Server ...

  3. 纯css的防止图片撑破页面的代码(图片自动缩放)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Remove-Azureaccount (Can't get Azure credentials to stick in Powershel)

    https://social.technet.microsoft.com/forums/azure/en-US/260df055-7c4e-4ce2-8f8d-190ad20a4b76/cant-ge ...

  5. https基础流程

    背景: https基于SSL,目的是保护http通信的过程,防止中间人篡改信息,或假冒服务端的问题.   要解决的问题: 1. 客户端如何证明是与正确的服务端进行通信 2. 客户端如何确认收到服务端的 ...

  6. Mysql查询重复记录

    第一步 使用group by 和 having cout 查找重复字段 SELECT t1.`order_book_id` FROM `quant_stock_info` t1 GROUP BY t1 ...

  7. 实时发布到tomcat

    当我们在运用eclipse进行web项目的文件编写的时候,希望编写的东西可以实时的发布在tomcat的webapps文件夹之中,那么我们应该怎么做呢!下面就是操作方法:   1. 首先你的eclips ...

  8. 转载:一幅图弄清DFT与DTFT,DFS的关系

    转载:http://www.cnblogs.com/BitArt/archive/2012/11/24/2786390.html 很多同学学习了数字信号处理之后,被里面的几个名词搞的晕头转向,比如DF ...

  9. Javascript > Eclipse > 自动代码规范化

    Reference: http://blog.csdn.net/jmyue/article/details/11060003 大项目往往是有很多人一起完成的,然而每个人都有自己的style,导致整个项 ...

  10. MVC的多表单

    中心思想就是在一个表单内不规定"action",在js里面用@Url.Axtion("视图层","控制器")方法来设置表单的传值. 控制器 ...