Android 的 GridLayout (网格布局)

文章介绍了Android4.0后引入的GridLayout布局,它允许自定义组件排列方式、行列数及组件跨行跨列。示例展示了如何创建一个计算器布局,并强调了使用时需注意GridLayout要求minSDK版本为14以上。对于低版本SDK的兼容,可以通过导入v7包的gridlayout实现。
摘要由CSDN通过智能技术生成

本节引言

今天要介绍的布局是Android 4.0以后引入的一个新的布局,和前面所学的TableLayout(表格布局) 有点类似,不过他有很多前者没有的东西,也更加好用,

  • 可以自己设置布局中组件的排列方式
  • 可以自定义网格布局有多少行,多少列
  • 可以直接设置组件位于某行某列
  • 可以设置组件横跨几行或者几列

另外,除了上述内容外,本节还会给大家使用gridLayout时会遇到的问题,以及如何解决低版本 sdk如何使用GridLayout的方法!接下来就开始本节的课程吧!


1.相关属性总结图


2.使用实例:计算器布局的实现:

运行效果图:

实现代码:

<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> 

代码解析: 代码很简单,只是回退与清楚按钮横跨两列,而其他的都是直接添加的,默认每个组件都是 占一行一列,另外还有一点要注意的: 我们通过:android:layout_rowSpanandroid:layout_columnSpan设置了组件横跨 多行或者多列的话,如果你要让组件填满横越过的行或列的话,需要添加下面这个属性: android:layout_gravity = "fill"!!!就像这个计算机显示数字的部分!


3.用法归纳:

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

  • step 1:先定义组件的对其方式 android:orientation 水平或者竖直,设置多少行与多少列
  • step 2:设置组件所在的行或者列,记得是从0开始算的,不设置默认每个组件占一行一列
  • step 3:设置组件横跨几行或者几列;设置完毕后,需要在设置一个填充:android:layout_gravity = "fill"

4.使用GridLayout要注意的地方:

因为GirdLayout是4.0后才推出的,所以minSDK版本要改为14或者以上的版本, 不然写布局代码的时候,这玩意就会莫名其妙地出错,说找不到这个GridLayout, 当然,如果你要低版本兼容的话,就要看下面的内容了!


5.低版本sdk如何使用GridLayout:

解决方法很简单:只需要导入v7包的gridlayout包即可! v7包一般在sdk下的:sdk\extras\android\support\v7\gridlayout 目录下 

<android.support.v7.widget.GridLayout>`
GridLayout 是一种 Android 中的布局方式,它可以将子控件按照网格的形式进行排列。与其他布局方式不同,GridLayout 不需要指定子控件的位置,而是通过设置行列数和对齐方式来完成布局的排列。 GridLayout 中的每个子控件都会被放置在一个网格单元格中,每个单元格可以包含一个子控件。GridLayout 可以设置行数和列数,以确定网格的大小。子控件可以跨越多个行或列,以实现更复杂的布局。 GridLayout 支持水平和垂直对齐方式,可以控制子控件在网格单元格中的对齐方式。除此之外,GridLayout 还支持权重和边距等常用属性,以实现更灵活的布局效果。 以下是一个 GridLayout 的示例代码: ``` <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="2" android:rowCount="3"> <TextView android:text="1" android:layout_gravity="fill" android:background="#ccc" android:layout_row="0" android:layout_column="0"/> <TextView android:text="2" android:layout_gravity="fill" android:background="#ccc" android:layout_row="0" android:layout_column="1"/> <TextView android:text="3" android:layout_gravity="fill" android:background="#ccc" android:layout_row="1" android:layout_column="0"/> <TextView android:text="4" android:layout_gravity="fill" android:background="#ccc" android:layout_row="1" android:layout_column="1"/> <TextView android:text="5" android:layout_gravity="fill" android:background="#ccc" android:layout_row="2" android:layout_column="0"/> <TextView android:text="6" android:layout_gravity="fill" android:background="#ccc" android:layout_row="2" android:layout_column="1"/> </GridLayout> ``` 以上代码会将 6 个 TextView 控件按照 2 列 3 行的方式进行排列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗者才子酒中仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值