四种基本布局之四——TableLayout

TableLayout 允许使用表格的方式来排列控件,这种布局也不常用,只需了解基本用法即可。
既然是表格,就一定会有行和列,再设计表格是应让每一行都有相同的列数,这样的表格也最简单。
当表格的某行一定要有不相等的列数时,就需要通过合并单元格的方式来应对。

设计一个登陆界面,允许用户输入账号密码后登陆

<TableLayout  xmlns: android = "http://schemas.android.com/apk/res/android"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent" >

    <TableRow>
    <TextView
        android :layout_height= "wrap_content"
        android :text= "Account" />
   
    <EditText 
        android :id= "@+id/account"
        android :layout_height= "wrap_content"
        android :hint= "Input you account" />
    </TableRow>
   
    <TableRow>
        <TextView
            android :layout_height= "wrap_content"
            android :text= "Passsword" />

        <EditText
            android :id= "@+id/password"
            android :layout_height= "wrap_content"
            android :hint= "textPassword" />
    </TableRow>
   
    <TableRow>
        <Button
            android :id= "@+id/login"
            android :layout_width= "wrap_content"
            android : layout_span = "2"
            android :text= "Login" />
    </TableRow> 
</TableLayout>


可以看出当前的登录界面并没有充分利用屏幕的宽度,右侧还空出了一块区域,因为在TableLayout中我们无法指定空间的宽度。
这时使用android:stretchColumns属性就可以很好地解决这个问题,它允许将TableLayout中的某一列进行拉伸,以达到自动适应屏幕宽度的作用。

<TableLayout  xmlns: android = "http://schemas.android.com/apk/res/android"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent"
    android :stretchColumns= "1" >


android:stretchColumns="1" 将android:stretchColumns的值指定为1,表示如果表格不能完全占满屏幕宽度,就将第二列进行拉伸。
指定为“1”就是拉伸第二列,指定为“0”就是拉伸第一列。

<TableLayout  xmlns: android = "http://schemas.android.com/apk/res/android"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent"
    android :stretchColumns= "0" >


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值