转自: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. OTG_FS_ID功能及引申

    1. 概要   OTG设备使用插头中的ID引脚来区分A/B Device,ID接地被称作为A-Device,充当USB Host,A-Device始终为总线   提供电力,ID悬空被称作为B-Devi ...

  2. Intent传递数据

    方式比较多,先看看代码,一会儿再总结. activity_main.xml <RelativeLayout xmlns:android="http://schemas.android. ...

  3. Wiki动画回顾系列序&&目录

    嘛,前前后后看了太多动画,我自己一直想做的事也是喜欢能做一款acg相关的应用,但一直没有好的点子,当然纠结到最后还是需要一个比较好的社区来让大家加入进来.一直有人让我给他们推番,而我也慢慢懂得“人心” ...

  4. Android中的颜色值RGB对照表表

    Android中颜色值是通过红(Red).绿(Green).蓝 (Blue)三原色,以及一个透明度(Alpha)值来表示的,颜色值总是以井号(#)开头,接下来就是Alpha-Red-Green-Blu ...

  5. leetcode–jump game II

    1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...

  6. POJ 2395 Out of Hay(MST)

    [题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...

  7. mysql如果搜索长度过宽 导致显示不全的情况解决

    今天我在搜索数据库里面优惠码字段 直接使用 select * from table 的命令的时候 由于 第一个 字段过长导致后面的都无法显示全..我还是宽屏! 所以 搜索了一下 可以让 它单行显示 使 ...

  8. ASP.NET自定义控件入门Demo

    最近看了MSDN关于自定义控件的介绍,根据官方的文档,自己学着做了一个简单的Demo给需要的朋友参考. ASP.NET 源生的TextBox是不带Label标签的,这里我要实现的是创建一个带Label ...

  9. fastscript调用delphi方法和DELPHI调用FASTSCRIPT方法

    fastscript调用Delphi过程:  1. 先创建事件处理方法:TfsCallMethodEvent 2. 然后再用调用TfsScript.AddMethod方法,第一个参数为Delphi方法 ...

  10. DIV 布局 左中右

    <style type="text/css">body{ margin:0; padding:0;}.Header{ height:100px; background: ...