转自:http://www.cnblogs.com/ghj1976/archive/2011/04/21/2023850.html

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TableLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<EditText android:id="@+id/et_led" android:layout_width="fill_parent"
android:digits="1234567890.+-*/=%\n\t()" android:text=""
android:layout_span="4" />
</TableRow>
<TableRow>
<Button android:text="(" android:id="@+id/btn_LeftParenthesis"></Button>
<Button android:text=")" android:id="@+id/btn_RightParenthesis"></Button>
<Button android:text="*" android:id="@+id/btn_x"></Button>
<Button android:text="back" android:id="@+id/btn_back"></Button>
</TableRow>
<TableRow>
<Button android:text="7" android:id="@+id/btn_7"></Button>
<Button android:text="8" android:id="@+id/btn_8"></Button>
<Button android:text="9" android:id="@+id/btn_9"></Button>
<Button android:text="%" android:id="@+id/btn_mod"></Button>
</TableRow>
<TableRow>
<Button android:text="4" android:id="@+id/btn_4"></Button>
<Button android:text="5" android:id="@+id/btn_5"></Button>
<Button android:text="6" android:id="@+id/btn_6"></Button>
<Button android:text="/" android:id="@+id/btn_div"></Button>
</TableRow>
<TableRow>
<Button android:text="1" android:id="@+id/btn_1"></Button>
<Button android:text="2" android:id="@+id/btn_2"></Button>
<Button android:text="3" android:id="@+id/btn_3"></Button>
<Button android:text="-" android:id="@+id/btn_sub"></Button>
</TableRow>
<TableRow>
<Button android:text="0" android:id="@+id/btn_0"></Button>
<Button android:text="." android:id="@+id/btn_dot"></Button>
<Button android:text="+" android:id="@+id/btn_plus"></Button>
<Button android:text="=" android:id="@+id/btn_equal"></Button>
</TableRow>
</TableLayout> </LinearLayout>

布局效果:

显然这不能满足我们的期望,下面我们演示 使用 android:stretchColumns 来自动拉伸

我们这里简单的给 TableLayout 增加一个属性 android:stretchColumns="*" 表示所有列都要自动拉伸,以便适应屏幕宽度。

布局效果

它的值即可以是数字,也可以是*,注意数字是从0开始的,即:android:stretchColumns="1" 是设置 TableLayout所有行的第二列为扩展列。

上面我们会看到 第四列的按钮比其他列的按钮要宽,如果我们想都一样宽如何办呢?

一个简单办法:

在自动拉伸的基础上,把每一列的宽度都设置成一样,比如下面的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TableLayout android:layout_width="match_parent"
android:layout_height="match_parent" android:stretchColumns="*">
<TableRow>
<EditText android:id="@+id/et_led" android:layout_width="fill_parent"
android:digits="1234567890.+-*/=%\n\t()" android:text=""
android:layout_span="4" />
</TableRow>
<TableRow>
<Button android:text="(" android:id="@+id/btn_LeftParenthesis"
android:layout_width="1dip"></Button>
<Button android:text=")" android:id="@+id/btn_RightParenthesis"
android:layout_width="1dip"></Button>
<Button android:text="*" android:id="@+id/btn_x"
android:layout_width="1dip"></Button>
<Button android:text="back" android:id="@+id/btn_back"
android:layout_width="1dip"></Button>
</TableRow>
<TableRow>
<Button android:text="7" android:id="@+id/btn_7"></Button>
<Button android:text="8" android:id="@+id/btn_8"></Button>
<Button android:text="9" android:id="@+id/btn_9"></Button>
<Button android:text="%" android:id="@+id/btn_mod"></Button>
</TableRow>
<TableRow>
<Button android:text="4" android:id="@+id/btn_4"></Button>
<Button android:text="5" android:id="@+id/btn_5"></Button>
<Button android:text="6" android:id="@+id/btn_6"></Button>
<Button android:text="/" android:id="@+id/btn_div"></Button>
</TableRow>
<TableRow>
<Button android:text="1" android:id="@+id/btn_1"></Button>
<Button android:text="2" android:id="@+id/btn_2"></Button>
<Button android:text="3" android:id="@+id/btn_3"></Button>
<Button android:text="-" android:id="@+id/btn_sub"></Button>
</TableRow>
<TableRow>
<Button android:text="0" android:id="@+id/btn_0"></Button>
<Button android:text="." android:id="@+id/btn_dot"></Button>
<Button android:text="+" android:id="@+id/btn_plus"></Button>
<Button android:text="=" android:id="@+id/btn_equal"></Button>
</TableRow>
</TableLayout> </LinearLayout>

这时候的效果就成了:

注意比起最初的就多了2个设置

android:layout_width="1dip" 和  android:stretchColumns="*"

Android 使用 TableLayout 布局拉伸宽度的更多相关文章

  1. Android 之 TableLayout 布局详解

    TableLayout简介 •简介 Tablelayout 类以行和列的形式对控件进行管理,每一行为一个 TableRow 对象,或一个 View 控件. 当为 TableRow 对象时,可在 Tab ...

  2. Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布局,AbsoluteLayout绝对布局,RelativeLayout相对布局

    在Android中提供了几个常用布局: LinearLayout线性布局 RelativeLayout相对布局 FrameLayout帧布局 AbsoluteLayout绝对布局 TableLayou ...

  3. Android之TableLayout表格布局

    1.相关属性 1.1.常用属性 android:collapseColumns 设置需要被隐藏的列的序列号 android:shrinkColumns 设置允许被收缩的列的序列号 android:st ...

  4. android:TableLayout表格布局详解

    1.TableLayout简介2.TableLayout行列数的确定3.TableLayout可设置的属性详解4.一个包含4个TableLayout布局的实例及效果图一.Tablelayout简介  ...

  5. 14.Android之Layout布局学习

    Android布局主要有5种,接下来学习总结下. 1) 最常见的线性布局 LinearLayout 线性布局是Android布局中最简单的布局,也是最常用,最实用的布局. android:orient ...

  6. [置顶] Android系统五大布局详解Layout

    我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit等 ...

  7. 【Android 复习】:Android五种布局的使用方法

    ---恢复内容开始--- 在Android布局中,有五种常用的布局,下面我们就来学习一下这几种布局的使用方式 1) 线性布局:LinearLayout 2) 帧布局:  FrameLayout 3)  ...

  8. Android系统五大布局详解Layout

    我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit ...

  9. Android视图篇之一:Android常见基本布局

    Android中,布局都是直接或间接的继承自ViewGroup类,其中,ViewGroup的直接子类目前有: AbsoluteLayout, AdapterView<T extends Adap ...

随机推荐

  1. 150个JS特效脚本

    收集了其它一些不太方便归类的JS特效,共150个,供君查阅. 1. simplyScroll simplyScroll这个jQuery插件能够让任意一组元素产生滚动动画效果,可以是自动.手动滚动,水平 ...

  2. scanf()/getchar()和gets()深入分析

    C/C++学习笔记1 - 深入了解scanf()/getchar()和gets()等函数 ---------------------------------------------------- | ...

  3. Java基础 —— CSS

    CSS:层叠样式表(Cascading Style Sheets) --> 提高显示功能,定义样式 html提供了div与span,只为了封装文本数据,div为一行数据,span为行内的数据. ...

  4. POJ 2653 Pick-up sticks(判断线段相交)

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7699   Accepted: 2843 De ...

  5. EIGRP默认路由分发的四种方法

    方式一:重发布静态 R2(config)#ip route 0.0.0.0 0.0.0.0 s1/1 R2(config)#router eig 10 R2(config-router)#redist ...

  6. 咏南C/S开发框架支持最新的DELPHI XE8开发

    特大好消息:咏南C/S开发框架支持最新的DELPHI XE8开发!咏南开发框架让你再无开发工具升级后顾之忧! 购买咏南开发框架送项目源码!

  7. HDU3535AreYouBusy(分组背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3535 分组背包,每一组加了以下三个限制 0 stands for the sets that should ch ...

  8. URAL 2068 Game of Nuts (博弈)

    题意:给定 n 堆石子,每次一个人把它们分成三堆都是奇数的,谁先不能分,谁输. 析:因为每堆都是奇数,那么最后肯定都是要分成1的,那么就把不是1的全加和,然后判断奇偶就OK了. 代码如下: #prag ...

  9. 判断时间大小 yyyy-MM-dd 格式

    // yyyy-MM-dd function bigThanToday(someDate){ var date = new Date(); var dateStr = date.getFullYear ...

  10. ecshop读写分离

    1.配置文件设置 $db_name = "ecshop"; $prefix = "ecs_"; $timezone = "Europe/Berlin& ...