GridLayout: GridLayout使用简介(转)[通俗易懂]

GridLayout: GridLayout使用简介(转)[通俗易懂]Android布局之GridLayout1GridLayout简介GridLayout是Android4.0新提供的网格矩阵形式的布局控件。GridLayout的继承关系如下:java.la

大家好,又见面了,我是你们的朋友全栈君。

Android 布局之GridLayout

 


1 GridLayout简介

GridLayout是Android4.0新提供的网格矩阵形式的布局控件。

GridLayout的继承关系如下:
java.lang.Object
  –> android.view.View
    –> android.view.ViewGroup
      –> android.widget.GridLayout

GridLayout包含的属性如下

android:alignmentMode
属性说明:当设置alignMargins,使视图的外边界之间进行校准。可以取以下值:
alignBounds — 对齐子视图边界。
alignMargins — 对齐子视图边距。

android:columnCount
属性说明:GridLayout的最大列数

android:rowCount
属性说明:GridLayout的最大行数

android:columnOrderPreserved
属性说明: 当设置为true,使列边界显示的顺序和列索引的顺序相同。默认是true。

android:orientation
属性说明:GridLayout中子元素的布局方向。有以下取值:
horizontal — 水平布局。
vertical — 竖直布局。

android:rowOrderPreserved
属性说明: 当设置为true,使行边界显示的顺序和行索引的顺序相同。默认是true。

android:useDefaultMargins
属性说明: 当设置ture,当没有指定视图的布局参数时,告诉GridLayout使用默认的边距。默认值是false。

这些是GridLayout布局本身的属性。

 

 


2 GridLayout子元素属性

上面描述的 GridLayout 的属性,是 GridLayout 布局本身的属性;下面 GridLayout 布局中的元素所支持的属性。GridLayout 布局中的元素的属性,定义在 GridLayout.LayoutParams 中。取值如下:

 

2.1 android:layout_column

属性说明: 显示该空间的列。例如,android:layout_column=”0″,表示在第1列显示该控件;android:layout_column=”1″,表示在第2列显示该控件。

layout文件示例,

GridLayout: GridLayout使用简介(转)[通俗易懂]
GridLayout: GridLayout使用简介(转)[通俗易懂]

复制代码
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:rowCount="2"
    android:columnCount="3" >
  <Button
        android:id="@+id/one"
        android:layout_column="1"
        android:text="1"/>
  <Button
        android:id="@+id/two"
        android:layout_column="0"
        android:text="2"/>
   <Button
        android:id="@+id/three"
        android:text="3"/>
  <Button
        android:id="@+id/devide"
        android:text="/"/>

</GridLayout>
复制代码

对应的显示效果图

GridLayout: GridLayout使用简介(转)[通俗易懂]

layout文件说明
android:orientation=”horizontal” — GridLayout中控件的布局方向是水平布局。
android:rowCount=”2″               — GridLayout最大的行数为2行。
android:columnCount=”3″          — GridLayout最大的列数为3列。
android:layout_column=”1″        — 定义控件one的位于第2列。
android:layout_column=”0″        — 定义该控two件的位于第1列。

 

2.2 android:layout_columnSpan

属性说明: 该控件所占的列数。例如,android:layout_columnSpan=”2″,表示该控件占2列。

layout文件示例

GridLayout: GridLayout使用简介(转)[通俗易懂]
GridLayout: GridLayout使用简介(转)[通俗易懂]

复制代码
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:rowCount="2"
    android:columnCount="3" >
  <Button
        android:id="@+id/one"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:text="1"/>
  <Button
        android:id="@+id/two"
        android:text="2"/>
   <Button
        android:id="@+id/three"
        android:text="3"/>
  <Button
        android:id="@+id/devide"
        android:text="/"/>

</GridLayout>
复制代码

对应的显示效果图

GridLayout: GridLayout使用简介(转)[通俗易懂]

layout文件说明

  数字”1″实际上占据的空间大小是2列,但是第2列显示为空白。若要第2列不显示空白,需要设置 android:layout_gravity属性,参考下例。

 

2.3 android:layout_row

属性说明: 该控件所在行。例如,android:layout_row=”0″,表示在第1行显示该控件;android:layout_row=”1″,表示在第2行显示该控件。它和 android:layout_column类似。

 

2.4 android:layout_rowSpan

属性说明: 该控件所占的行数。例如,android:layout_rowSpan=”2″,表示该控件占2行。它和 android:layout_columnSpan类似。

 

2.5 android:layout_gravity

属性说明

该控件的布局方式。可以取以下值:
  top                      — 控件置于容器顶部,不改变控件的大小。
  bottom                — 控件置于容器底部,不改变控件的大小。
  left                     — 控件置于容器左边,不改变控件的大小。
  right                   — 控件置于容器右边,不改变控件的大小。
  center_vertical     — 控件置于容器竖直方向中间,不改变控件的大小。
  fill_vertical          — 如果需要,则往竖直方向延伸该控件。
  center_horizontal — 控件置于容器水平方向中间,不改变控件的大小。
  fill_horizontal      — 如果需要,则往水平方向延伸该控件。
  center                — 控件置于容器中间,不改变控件的大小。
  fill                     — 如果需要,则往水平、竖直方向延伸该控件。
  clip_vertical        — 垂直剪切,剪切的方向基于该控件的top/bottom布局属性。若该控件的gravity是竖直的:若它的gravity是top的话,则剪切该控件的底部;若该控件的gravity是bottom的,则剪切该控件的顶部。
  clip_horizontal     — 水平剪切,剪切的方向基于该控件的left/right布局属性。若该控件的gravity是水平的:若它的gravity是left的话,则剪切该控件的右边;若该控件的gravity是  right的,则剪切该控件的左边。
  start                  — 控件置于容器的起始处,不改变控件的大小。
  end                   — 控件置于容器的结束处,不改变控件的大小。

对应函数: setGravity(int)

layout文件示例:

GridLayout: GridLayout使用简介(转)[通俗易懂]
GridLayout: GridLayout使用简介(转)[通俗易懂]

复制代码
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:rowCount="2"
    android:columnCount="3" >
  <Button
        android:id="@+id/one"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="1"/>
  <Button
        android:id="@+id/two"
        android:text="2"/>
   <Button
        android:id="@+id/three"
        android:text="3"/>
  <Button
        android:id="@+id/devide"
        android:text="/"/>

</GridLayout>
复制代码

对应的显示效果图

GridLayout: GridLayout使用简介(转)[通俗易懂] 

 

 


3 应用示例

定义一个简单的计算器界面,包含“0-9、.、+、-、*、/、=、”。用GridLayout实现。

layout文件 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!-- GridLayout: 5行 4列 水平布局 -->
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:rowCount="5"
    android:columnCount="4" >
  <Button
        android:id="@+id/one"
        android:text="1"/>
  <Button
        android:id="@+id/two"
        android:text="2"/>
   <Button
        android:id="@+id/three"
        android:text="3"/>
  <Button
        android:id="@+id/devide"
        android:text="/"/>
  <Button
        android:id="@+id/four"
        android:text="4"/>
  <Button
        android:id="@+id/five"
        android:text="5"/>
  <Button
        android:id="@+id/six"
        android:text="6"/>
  <Button
        android:id="@+id/multiply"
        android:text="×"/>
  <Button
        android:id="@+id/seven"
        android:text="7"/>
  <Button
        android:id="@+id/eight"
        android:text="8"/>
  <Button
        android:id="@+id/nine"
        android:text="9"/>
    <Button
        android:id="@+id/minus"
        android:text="-"/>
    <Button
        android:id="@+id/zero"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="0"/>
  <Button
        android:id="@+id/point"
        android:text="."/>
    <Button
        android:id="@+id/plus"
        android:layout_rowSpan="2"
        android:layout_gravity="fill"
        android:text="+"/>
    <Button
        android:id="@+id/equal"
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="="/> 
</GridLayout>
复制代码

 

点击下载:源代码

效果图:

GridLayout: GridLayout使用简介(转)[通俗易懂]

 

本文转自:Android 布局之GridLayout

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155158.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • axure快速原型设计工具

    axure快速原型设计工具

    2022年2月22日
    52
  • tomcat查看日志[通俗易懂]

    tomcat查看日志[通俗易懂]随笔-168文章-1评论-8linux下实时查看tomcat运行日志 1、先切换到:cdhome/tomcat/logs 2、tail-fcatalina.out 3、这样运行时就可以实时查看运行日志了 4.Ctrl+c是退出tail命令 eg: [root@TEST-Alogs]#ls catalina.20…

    2022年6月20日
    33
  • Web安全之业务逻辑漏洞

    Web安全之业务逻辑漏洞业务逻辑不同的项目有不同的功能,不同的功能需要不同的代码实现,实现这些核心功能的代码就叫业务逻辑。业务逻辑漏洞业务逻辑漏洞是指由于程序逻辑不严谨或逻辑太复杂,导致一些逻辑分支不能正常处理或处理错误。常见的业务逻辑漏洞业务逻辑漏洞挖掘过程确定业务流程—>寻找流程中可以被操控的环节—>分析可被操控环节中可能产生的逻辑问题—>尝试修改参数触发逻辑问题业务逻辑漏洞1.URL跳转漏洞1.1.URL跳转概述1.2.触发方式及绕或技巧1.3.修复方法2.短信邮箱轰炸漏洞2.1.短信邮.

    2022年6月1日
    32
  • vue封装组件以及调用「建议收藏」

    vue封装组件以及调用「建议收藏」1.index.jsexport{defaultasaddDialog}from’./addDialog’export{defaultasEditDialog}from’./EditDialog’editDialog.vue<template><div><el-dialogtitle=”编辑”:visible.sync=”dialogVisible”width=”40%”

    2022年9月23日
    1
  • k8s(十)基本存储[通俗易懂]

    k8s(十)基本存储[通俗易懂]文章目录概述EmptyDirHostPathNFSk8s的数据存储概述在前面已经提到,容器的生命周期可能很短,会被频繁的创建和销毁。那么容器在销毁的时候,保存在容器中的数据也会被清除。这种结果对用户来说,在某些情况下是不乐意看到的。为了持久化保存容器中的数据,kubernetes引入了Volume的概念。Volume是Pod中能够被多个容器访问的共享目录,它被定义在Pod上,然后被一个Pod里面的多个容器挂载到具体的文件目录下,kubernetes通过Volume实现同一个Pod中不同容器之间的数据

    2022年8月9日
    0
  • linux 网口_网络管理员网址

    linux 网口_网络管理员网址Linux12:网络管理文章目录一.网络接口名称规则1.认识网卡2.修改网卡配置文件3.主机名4.网络测试工具一.网络接口名称规则计算机与外界通信的前提是:有网卡(网络适配器NAT)注意:虚拟机的总内存,不能超过真机服务器和客户机:谁提供服务,谁就是服务器1.认识网卡#ls /etc/sysconfig/network-scripts/补充:网络管理器NetworkManager,动态网络的控制器与配置系统,它用于当网络设备可用时保持设备和连接开启并激活查看该服务的运行状况:#

    2022年8月9日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号