GridLayout(网格布局)
2017-03-22 20:55
85 查看
常用属性:
排列对齐:
①设置组件的排列方式: android:orientation="" vertical(竖直,默认)或者horizontal(水平)②设置组件的对齐方式: android:layout_gravity="" center,left,right,buttom啊,这些,如果想同时用两种的话:eg: buttom|left
设置布局为几行几列:
①设置有多少行:android:rowCount="4" //设置网格布局有4行②设置有多少列:android:columnCount="4" //设置网格布局有4列
设置某个组件位于几行几列
注:都是从0开始算的哦!①组件在第几行:android:layout_row = "1" //设置组件位于第二行
②组件在第几列:android:layout_column = "2" //设置该组件位于第三列
设置某个组件横跨几行几列:
①横跨几行:android:layout_rowSpan = "2" //纵向横跨2行②横跨几列:android:layout_columnSpan = "3" //横向横跨2列
使用实例:
最最最普遍的例子----计算器界面:
效果图:PS:这里要说一点,网格布局和其他布局不同,可以不为组件设置Layout_width和Layout_height属性
因为组件的宽高由几行几列决定了,当然,你也可以写个wrap_content
代码:
[html] view plain copy
<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:rowCount="6"
android:columnCount="4"
android:orientation="horizontal">
<TextView
android:layout_columnSpan="4"
android:text="0"
android:textSize="50sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
<Button
android:text="回退"
android:layout_columnSpan="2"
android:layout_gravity="fill"
/>
<Button
android:text="清空"
android:layout_columnSpan="2"
android:layout_gravity="fill"
/>
<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_rowSpan和android:layout_columnSpan设置表明组件横越的行数与列数
再通过:android:layout_gravity = "fill" 设置表明组件填满所横越的整行或者整列
用法总结:
①GridLayout使用虚细线将布局划分为行,列和单元格,同时也支持在行,列上进行交错排列②使用流程:
step 1:先定义组件的对其方式 android:orientation 水平或者竖直
step 2:设置组件所在的行或者列,记得是从0开始算的
step 3:设置组件横跨几行或者几列;设置完毕后,需要在设置一个填充:android:layout_gravity = "fill"
另外:这些属性也常用到:
<GridView android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="35px" <!-- grid元素之间的竖直间隔 -->
android:horizontalSpacing="5px"
<!--grid元素之间的水平间隔 -->
android:numColumns="auto_fit" <!--表示有多少列,如果设置为auto_fit,将根据columnWidth和Spacing来自动计算 -->
android:columnWidth="100px" <!-- 一般建议采用有像素密度无关的dip或者dp来表示-->
android:stretchMode="columnWidth"
<!--如何填满空余的位置,模拟器采用WVGA800*480,每排4列,有4*100+5*3=415,还余65px的空间,如果是columnWidth,则这剩余的65将分摊给4列,每列增加16/17px。如果采用SpacingWidth,则分摊给3个间隔空隙
-->
android:gravity="center" />
相关文章推荐
- Android4.0新增布局GridLayout(网格布局)
- Java图形化界面设计——布局管理器之GridLayout(网格布局)
- 02网格布局Gridlayout
- Android中使用GridLayout网格布局来制作简单的计算器App
- 【Gridlayout网格布局,Framlayout帧布局】
- 使用StaggeredGridLayoutManager实现交错式网格布局
- 网格布局(GridLayout)
- [置顶] Android--(9)--详解网格布局(GridLayout)
- android之网格布局GridLayout
- FlowLayout流式布局管理器与网格布局GridLayout
- Java图形化界面设计——布局管理器之GridLayout(网格布局)
- (GridLayout)网格布局 :网格的世界更精彩
- 02网格布局Gridlayout
- Android基础入门教程——2.2.5 GridLayout(网格布局)
- android 4.0引入的新布局GridLayout(网格布局)
- Android零基础入门第32节:新推出的GridLayout网格布局
- Android实现计算器布局(四种布局方式)之GridLayout网格布局
- GridLayout网格布局
- Java开发GUI之GridLayout网格布局