Android应用开发之GridLayout(网格布局)_android gridlayout-程序员宅基地

技术标签: android  Android应用开发技术  

在这里插入图片描述

GridLayout

网格布局是Android 4.0以后引入的一个新的布局.

它和前面所学的TableLayout(表格布局) 有点类似,不过有很多前者没有的东西,也更加好用.

特点:

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

常用属性

GridLayout属性:

属性 作用
android:columnCount 最大列数
android:rowCount 最大行数
android:orientation GridLayout中子元素的布局方向
android:alignmentMode alignBounds:对齐子视图边界 alignMargins :对齐子视距内容,默认值
android:columnOrderPreserved 使列边界显示的顺序和列索引的顺序相同,默认是true
android:rowOrderPreserved 使行边界显示的顺序和行索引的顺序相同,默认是true
android:useDefaultMargins 没有指定视图的布局参数时使用默认的边距,默认值是false

子元素属性:

属性 作用
android:layout_column 指定该单元格在第几列显示
android:layout_row 指定该单元格在第几行显示
android:layout_columnSpan 指定该单元格占据的列数
android:layout_rowSpan 指定该单元格占据的行数
android:layout_gravity 指定该单元格在容器中的位置
android:layout_columnWeight (API21加入)列权重
android:layout_rowWeight (API21加入) 行权重

GridLayout在API21时引入了android:layout_columnWeight和android:layout_rowWeight来解决平分问题

使用layout_columnSpanlayout_rowSpan时要加上layout_gravity属性,否则没有效果

实例代码

activity_main.xml:

<?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:columnCount="4"
    android:rowCount="5"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_rowWeight="1"
        android:text="Button0" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="1"
        android:layout_rowWeight="1"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="2"
        android:layout_rowWeight="1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="1"
        android:layout_rowWeight="1"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:layout_rowWeight="1"
        android:text="Button4" />

</GridLayout>

运行效果:

请添加图片描述

今天的分享就回到这里了 ^_^

在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012739527/article/details/123894081

智能推荐

进程与线程的联系与区别_进程与线程的区别与联系-程序员宅基地

文章浏览阅读983次。一、基本概念_进程与线程的区别与联系

vue实现页面内容禁止选中,仅输入框和文本域可选_禁止输入框文件选中-程序员宅基地

文章浏览阅读7.9k次,点赞6次,收藏8次。上网上翻了翻,共找到两种方式CSS样式控制,只需将下面代码复制到 vue应用下,index.html文件中的body标签上*{ -webkit-touch-callout:none; /*系统默认菜单被禁用*/ -webkit-user-select:none; /*webkit浏览器*/ -khtml-user-select:none; /*早期浏览器..._禁止输入框文件选中

(笔记)微信开发笔记之微信授权_wxservice.checksignature false-程序员宅基地

文章浏览阅读802次。准备工作 申请一个微信测试号 下载一个内网穿透软件用于获取域名 我用的多 wenat软件诺依的开源项目微信授权接口配置位置测试号管理的接口配置中 此处使用内网穿透转换的域名/** * @desc: 门户入口 * @author: cao_wencao * @date: 2019-09-02 17:06 */@Slf4j@AllArgsConstructor@Rest..._wxservice.checksignature false

简单说说NVMe 2.0协议_nvme2.0-程序员宅基地

文章浏览阅读5.7k次,点赞2次,收藏21次。简单介绍一下2022年1月最新的NVMe协议_nvme2.0

微信公众号只能设置两个网页授权域名的解决方案-程序员宅基地

文章浏览阅读5.1k次,点赞2次,收藏10次。中转站index.php<?phpif(isset($_GET['param']) && !empty($_GET['param'])){ $param = urldecode($_GET['param']); if(!strpos($param, '?')){ $action = "huidiao.php?param=" .$param.'?1=1'; }else{ $action = "huidiao.php?par

完整版:献给初学者的Java学习方法-程序员宅基地

文章浏览阅读77次。2019独角兽企业重金招聘Python工程师标准>>> ..._java给初学者最好理解的讲义

随便推点

【概率论】五分钟搞懂概率论联合密度函数与概率之间的计算-程序员宅基地

文章浏览阅读4.1w次,点赞19次,收藏43次。【例题】设X和Y的联合密度函数为:f(x,y)={20<x<∞,0<y<∞0其他区域f(x,y)=\left\{\begin{array}{rcl}2 && {0<x<\infty,0<y<\infty}\\0 && 其他区域\end{array}\right.f(x,y)={20​​0<x<∞,0<y<∞其他区域​计算(1)P{X>1,Y<1} (2)P{X<Y}类_联合密度函数

useEffect里面setState获取不到异步的数据_useeffect setstate异步-程序员宅基地

文章浏览阅读2.5k次。useEffect里面setState获取不到异步的数据_useeffect setstate异步

Unity按键设置-InputManager_unity axis snap-程序员宅基地

文章浏览阅读9.9k次,点赞3次,收藏15次。unity的Axis设置,路径为Edit-&gt;Project setting-&gt;Input:属性如下:Name:【按键名】该键的名称,可以在脚本编程中直接引用他。比如:Input.GetButtonDown(“Jump”);Descriptive Name:【描述名】在游戏的独立机构中的配置对话框中,当控制值为正时候所显示的名称。默认空白。Negative Descrip..._unity axis snap

QML自定义Qt Quick Controls 2 样式之 RadioButton_qml radiobutton 自定义-程序员宅基地

文章浏览阅读3k次。近期学习Qt,记录下学习过程,以免忘记。操作环境 WIN7+Qt 5.12.0。新建资源目录 controls,新建 RadioButton.qml文件。RaidoButton包含三个可视项目:background, content item 和 indicator,在默认样式文件中复制相关代码。默认样式文件路径:$QTDIR/qml/QtQuick/Controls.2/RadioButt..._qml radiobutton 自定义

类模板分文件编写遇到的问题和解决方法_类模板函数分文件时为什么调用不到-程序员宅基地

文章浏览阅读425次。1.问题引入: 当使用类模板的成员函数的具体实现与声明分别写在不同的文件时,出现错误,使用vs作为IDE。错误代码:main.cpp#include"person.h"void test01() { Person<string, int> p("赵云", 38); p.showPerson();}int main() { test01(); return 0;..._类模板函数分文件时为什么调用不到

豆沙绿色值---全_豆沙绿参数-程序员宅基地

文章浏览阅读6.9k次。豆沙绿的参数为:a. RGB颜色:(199,237,204)b. 十六位颜色代码:C7EDCCc. 色调(Hue)85、饱和度(Sat)123、亮度(Lum)205------------- The End -------------许可协议: 转载请保留原文链接及作者。..._豆沙绿参数

推荐文章

热门文章

相关标签