您的位置:首页 > 移动开发 > Android开发

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

2016-10-08 20:13 225 查看
问题描述:这是一个简单的网格布局应用,计算器界面,代码如下:
<? 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"

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  GridLayout 网格布局