android中网格布局组件溢出原因

问题描述:这是一个简单的网格布局应用,计算器界面,代码如下:

<? xml version= "1.0" encoding= "utf-8" ?>
< GridLayout xmlns: android = "http://schemas.android.com/apk/res/android"
    xmlns: tools = "http://schemas.android.com/tools"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent"
    android :rowCount= "6"
    android :columnCount= "4"
    tools :context= "com.lifei.helloworld.TestGrid">
    <!-- 定义一个横跨 4 列的文本框,并定义前景色和背景色等属性 -->
    < TextView
        android:id= "@+id/t1"
        android:layout_width= "match_parent"
        android:layout_height= "wrap_content"
        android:textSize= "50sp"
        android:padding= "3pt"
        android:background= "#eee"
        android:textColor= "#000"
        android:text= "0" />
    <Button
        android:id= "@+id/clear"
        android:layout_width= "match_parent"
        android:layout_height= "wrap_content"
        android:layout_columnSpan= "4"
        android:text= " 清除 " />
    <Button
        android:id= "@+id/c1"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:layout_column= "0"
        android:layout_row= "2"
        android:text= "1" />
    <Button
        android:id= "@+id/c2"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:layout_column= "1"
        android:layout_row= "2"
        android:text= "2" />
    <Button
        android:id= "@+id/c3"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:layout_column= "2"
        android:layout_row= "2"
        android:text= "3" />
    <Button
        android:id= "@+id/c4"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:layout_column= "3"
        android:layout_row= "2"
        android:text= "4" />

</GridLayout >

图形界面如下,可以看到,按钮一还在,但是其余的按钮在外面:

                
原因分析:能让按钮1占一行的可能原因是,该列的宽度由该列中最宽的组件决定,
                  而按钮1所在的第0列,还包含上面一个TextView和一个“清除按钮”,
                  所以,
                  ①在TextView中补一句: android:layout_columnSpan= "4",那么第0列
                  TextView变成了占4列的TextView了
                  ②如果你连“1”号按钮也占一行,请把相应属性改 为:
                  android:layout_width= "wrap_content"
        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值