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 ...
随机推荐
- VS如何设置OpenCV静态编译
可以使用opencv提供的静态链接库也可以自己编译静态链接库. 1 使用opencv提供的静态链接库,位置如下图. 首先设置VS配置.有如下几个配置 1 工具->选项->项目和解决方案 ...
- html --- canvas --- javascript --- 在线画板
canvas功能十分强大,制作一个简易画板易如反掌,主要涉及canvas的画线能力,javascript鼠标点击事件 如有问题请参考:http://www.html5party.com/857.htm ...
- 二.JSP开发过程中遇到的问题及解决
一.开发环境问题 问题一:Failed to load the JNI shared library 启动Eclipse时弹出“Failed to load the JNI shared librar ...
- C++ 串
♣ string 的基类basic_string中没有虚函数,它无意成为基类.更像是为了处理字符相关的问题而专门提供的一个工具及操作方法.如:想要在一个字符串str1中查找str2,没必要每次都去写K ...
- 关于Windows API、CRT和STL二三事
1.本文编写目的 本文是为了帮助一些人弄清一些关于Windows API, C运行时程序库(CRT), 和标准C++库(STL)的基本概念.有很多人甚至是有经验的程序员在这些概念上是含糊不清的甚 ...
- 浅谈w3c标准
#浅谈w3c标准 ##w3c标准是什么 w3c标准包括多个方面,官方是从应用角度分的,相关的文档可以戳[这里](http://www.w3.org/standards/).如果从WEB技术角度,可以分 ...
- 【转】内网yum源搭建
我们内网yum要玩的话,先加hosts,然后找运维要CentOS_base.repo这个文件,然后yum clean all && yum makecache ========== ...
- 第二百二十八天 how can I 坚持
hibernate 还有好多不会搞啊,本来很简单的东西,没用过就不会. 今天... 只是感觉很累,昨天爬山爬的,不知道该写点啥了,买的羽绒服到了,还行吧,凑合穿吧. 睡觉了.今天貌似又发脾气了.哎.. ...
- SpringMVC+JPA使用注入的方式环境搭建
----------------------------------------------------DAO--------------------------------------------- ...
- UI:数据持久化
数据持久化 参考1 参考2 参考3 什么是数据持久化,就是将文件保存在本地的硬盘中,使得应用程序或者机器重启后可以继续访问以前保留的数据.IOS开发中有许多的数据持久化方案. 如下面五种方案 ...