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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 重磅官宣:Nacos2.0性能提升10倍[通俗易懂]

    重磅官宣:Nacos2.0性能提升10倍[通俗易懂]简介:​Nacos2.0作为一个跨代版本,彻底解决了Nacos1.X的性能问题,将性能提升了10倍。作者:席翁继Nacos1.0发布以来,Nacos迅速被成千上万家企业采用,并构建起强大的生态。但是随着用户深入使用,逐渐暴露一些性能问题,因此我们启动了Nacos2.0的隔代产品设计,时隔半年我们终于将其全部实现,实测性能提升10倍,相信能满足所有用户的性能需求。下面由我代表社区为大家介绍一下这款跨代产品。Nacos简介Nacos是一个更易于构建云原生应用的动态服务发现、配置管理

    2026年2月2日
    4
  • linux常用命令解释_vim常用命令总结

    linux常用命令解释_vim常用命令总结记住,是小写的L,表示使用列表的方式来列出目录中的内容,ls-l这是常用的命令,一般也是可以这里的/是Linux上的特殊目录,称为”根目录”,相当于windows的”此电脑”,这里的这些目录,就相当于”系统文件”他就是能告诉我们当前是在哪个目录里,因为我们在使用命令的时候,有的时候,目录是比较复杂的,容易不记得自己在哪里(迷路了)显示了当前目录所对应的绝对路径,在windows上,就是以盘符开头的路径的绝对路径,在Linux上,是以/开头(根目录)就是绝对路径cd后

    2025年11月22日
    4
  • 对L1正则化和L2正则化的理解[通俗易懂]

    一、奥卡姆剃刀(Occam’srazor)原理:     在所有可能选择的模型中,我们应选择能够很好的解释数据,并且十分简单的模型。从贝叶斯的角度来看,正则项对应于模型的先验概率。可以假设复杂模型有较小的先验概率,简单模型有较大的先验概率。  二、正则化项     2.1、什么是正则化?   正则化是结构风险最小化策略的实现,在经验风险上加一个正则项或罚项,正则项一共有两种L1…

    2022年4月11日
    189
  • Negative Sampling 负采样详解[通俗易懂]

    Negative Sampling 负采样详解[通俗易懂]在word2vec中,为了简化训练的过程,经常会用到NegativeSampling负采样这个技巧,这个负采样到底是怎么样的呢?之前在我的博文word2vec算法理解和数学推导中对于word2vec有了很详细的数学推导,这里主要讲解一下负采样是如何降低word2vec的复杂度的。首先我们直接写出word2vec的目标函数,假设有一句话:query=w1,w2,w3,..,wnquery=…

    2022年6月26日
    86
  • HTML如何实现页面跳转(html跳转到指定页面)

    1、html中使用meta中跳转,通过meta可以设置跳转时间和页面&lt;head&gt;&lt;!–只是刷新不跳转到其他页面–&gt;&lt;metahttp-equiv="refresh"content="5"&gt;&lt;!–定时转到其他页面–&gt;&lt;metahttp-equiv="refresh&quot

    2022年4月11日
    2.0K
  • B样条曲线(B-spline Curves)

    B样条曲线(B-spline Curves)关键字:NURBS,基函数,控制点,节点,另一个讲的很好的https://www.cnblogs.com/icmzn/p/5100761.html看了网上很多相关资料才得以下笔,资料太多,这里就不一一列举了,感谢各位大佬的资料本博客顺序不太好,看前面的东西可能需要提前看后面的东西。正在努力修炼,敬请谅解写了个B样条曲线计算的完成程序,包括绘图,https://download.c…

    2022年6月18日
    34

发表回复

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

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