Android 使用 TableLayout 布局拉伸宽度
转自: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 布局拉伸宽度的更多相关文章
- Android 之 TableLayout 布局详解
TableLayout简介 •简介 Tablelayout 类以行和列的形式对控件进行管理,每一行为一个 TableRow 对象,或一个 View 控件. 当为 TableRow 对象时,可在 Tab ...
- Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布局,AbsoluteLayout绝对布局,RelativeLayout相对布局
在Android中提供了几个常用布局: LinearLayout线性布局 RelativeLayout相对布局 FrameLayout帧布局 AbsoluteLayout绝对布局 TableLayou ...
- Android之TableLayout表格布局
1.相关属性 1.1.常用属性 android:collapseColumns 设置需要被隐藏的列的序列号 android:shrinkColumns 设置允许被收缩的列的序列号 android:st ...
- android:TableLayout表格布局详解
1.TableLayout简介2.TableLayout行列数的确定3.TableLayout可设置的属性详解4.一个包含4个TableLayout布局的实例及效果图一.Tablelayout简介 ...
- 14.Android之Layout布局学习
Android布局主要有5种,接下来学习总结下. 1) 最常见的线性布局 LinearLayout 线性布局是Android布局中最简单的布局,也是最常用,最实用的布局. android:orient ...
- [置顶] Android系统五大布局详解Layout
我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit等 ...
- 【Android 复习】:Android五种布局的使用方法
---恢复内容开始--- 在Android布局中,有五种常用的布局,下面我们就来学习一下这几种布局的使用方式 1) 线性布局:LinearLayout 2) 帧布局: FrameLayout 3) ...
- Android系统五大布局详解Layout
我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit ...
- Android视图篇之一:Android常见基本布局
Android中,布局都是直接或间接的继承自ViewGroup类,其中,ViewGroup的直接子类目前有: AbsoluteLayout, AdapterView<T extends Adap ...
随机推荐
- linux modprobe.conf怎么不见了—-CentOS 6
用习惯了CentOS或RHEL的用户或许很熟悉/etc/modprobe.conf文件,系统声卡.网卡.SCSI卡的驱动类型都在这里定义,同样部分动态加载的模块的参数也可以在这个文件中定义,但是在Ce ...
- atoi()函数的实现
atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...
- 关于WordPress建站的原理二三事
在写关于仿站文章详情页如何制作之前,我觉得有必要就一些原理性的问题,做一些说明.文章详情页的核心模块和首页有很多相似的地方,比如调用文章的标题.文章的内容.文章分类.作者等,实现起来都差不多,因此,了 ...
- 【Python学习笔记】字典操作
字典dict是Python中唯一内置的映射类型,由键值对组成,字典是无序的.字典的键必须是不变对象,如字符串.数字.元组等,而包含可变对象的列表.字典和元组则不能作为键.这里可变和不可变的意思是指这个 ...
- html5 拖拽
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- always NetWork Performance measure Tools
1,iperf key feature:Measuring TCP and UDP BandWidth Performance Iperf features; *TCP .Measure bandwi ...
- cocos2d-x 3.2 例子文件工程的位置
更新到3.2后突然想要看看官方的例子,忽然发现在test中的cpp工程下面没有了工程的启动配置.奇怪,难道只有代码吗?重新查找后原来启动的工程文件都移动到了build文件夹下面,具体的路径就是 coc ...
- [翻译]Behavior-Driven Development (BDD)行为驱动开发(二)
测试驱动开发体现了开发人员对软件产品各部分运作方式的理解,而行为驱动开发则关注于开发人员对软件产品最终表现的行为的预期. 行为驱动开发 TDD更像是一种范式而不是一个过程.它描述了一种先编写测试,然后 ...
- [原创]Devexpress XtraReports 系列 9 创建邮件合并报表
昨天发表了Devexpress XtraReports系列第八篇[原创]Devexpress XtraReports 系列 8 创建Drill-Through报表,今天我们继续. 今天的主题是创建邮件 ...
- Struts2数据校验方法
方法: 1.在Action类中execute()方法中进行校验. 优点:Action类无需继承框架中的类. 缺点:(1)当有多个校验时,代码重复,违反高内聚,低耦合. 2.重写框架ActionSupp ...