从零开始学android编程之网格布局管理器(2-2)

2 指定单个组件在网格中的位置

可以通过指定组件的android:layout_column属性和android:layout_row属性来指定该组件在网格中的行数和列数。与android:columnCount属性和android:rowCount属性类似,android:layout_column属性和android:layout_row属性也与android:orietation属性有关。当android:orietation属性值是“horizontal”时,android:columnCount属性值有效;当android:orietation属性值是“vertical”时,android:rowCount属性值有效。代码如下所示

<GridLayout

........
    android:orientation="vertical"
    android:rowCount="3"
    
>

..........
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按键6"
        android:layout_row="0"
/>
</GridLayout>

“按键6”的原始位置在第3行第2列,通过android:rowCount属性将“按键6”的位置设置到第1列,此时的效果如图3所示。

 


也可以通过android:columnCount属性修改组件所在的列数,需要注意的是,此时android:orietation属性值必须是“horizontal”。

3 设置组件所占的宽度和高度

通过android:layout_rowSpanandroid:layout_columnSpan来指定组件所占的高度和宽度。与上述属性类似,当android:orietation属性值是“horizontal”时,android:layout_columnSpan属性值有效;当android:orietation属性值是“vertical”时,android:layout_rowSpan属性值有效。

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:columnCount="2"
    
>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:text="按键1"
/>
</GridLayout>

以上代码将网格分为2列,并且将“按键1”的所占的宽度设置为2列,其效果如图4所示。

 


从图4中可以看出,“按键1”占据了2列的位置,之所以“按键1”的宽度会延伸到整个屏幕,原因是其android:layout_width属性值设置为“match_parent”。

使用TableLayout表格布局管理器来设计外形规则的表格,使用GridLayout网格布局管理器来设置外形不规则的表格。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值