美文网首页
布局ViewGroup原理解析(五):GridLayout

布局ViewGroup原理解析(五):GridLayout

作者: bug音音 | 来源:发表于2021-01-07 11:28 被阅读0次
image

1、实例

image

实现代码:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/GridLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:columnCount="4" android:orientation="horizontal" android:rowCount="6" > <TextView android:layout_columnSpan="4" android:layout_gravity="fill" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#FFCCCC" android:text="0" android:textSize="50sp" /> <Button android:layout_columnSpan="2" android:layout_gravity="fill" android:text="回退" /> <Button android:layout_columnSpan="2" android:layout_gravity="fill" android:text="清空" /> <Button android:text="+" /> <Button android:text="1" /> <Button android:text="2" /> <Button android:text="3" /> <Button android:text="-" /> <Button android:text="4" /> <Button android:text="5" /> <Button android:text="6" /> <Button android:text="*" /> <Button android:text="7" /> <Button android:text="8" /> <Button android:text="9" /> <Button android:text="/" /> <Button android:layout_width="wrap_content" android:text="." /> <Button android:text="0" /> <Button android:text="=" /></GridLayout>

2、用法归纳:

当通过:android:layout_rowSpanandroid:layout_columnSpan设置了组件横跨多行或者多列的时,如果要让组件填满横越过的行或列的话,需要添加下面这个属性: android:layout_gravity = "fill"

①GridLayout使用虚细线将布局划分为行,列和单元格,同时也支持在行,列上进行交错排列 ②使用流程:

step 1:先定义组件的对其方式 android:orientation 水平或者竖直,设置多少行与多少列

step 2:设置组件所在的行或者列,记得是从0开始算的,不设置默认每个组件占一行一列

step 3:设置组件横跨几行或者几列;设置完毕后,需要在设置一个填充:android:layout_gravity = "fill"

3、使用GridLayout要注意的地方:

因为GirdLayout是4.0后才推出的,所以minSDK版本要改为14或者以上的版本,不然写布局代码的时候会出错,低版本sdk要使用GridLayout,只需要导入v7包的gridlayout包即可。v7包一般在sdk下的:sdk\extras\android\support\v7\gridlayout目录下,下载: gridlayout_v7_jay.rar ,标签写法:<android.support.v7.widget.GridLayout>

文末

欢迎关注我的简书,分享Android干货,交流Android技术。
对文章有何见解,或者有何技术问题,都可以在评论区一起留言讨论,我会虔诚为你解答。
最后,如果你想知道更多Android的知识或需要其他资料我这里均免费分享,只需你多多支持我即可哦!

——可以直接点这里可以看到全部资料内容免费打包领取。

图.jpeg

相关文章

网友评论

      本文标题:布局ViewGroup原理解析(五):GridLayout

      本文链接:https://www.haomeiwen.com/subject/dinxaktx.html