您的位置:首页 > 移动开发 > Android开发

Android开发总结笔记 GirdLayout(网格布局) 1-1-5

2015-09-22 16:33 543 查看
GridLayout的继承结构





网格布局是andorid4.0后才出来的一个布局管理器,不过现在v7包里面有兼容的





网格布局可以指定行数和列数





下面来试试这些属性

<GridLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_blue_bright"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_dark"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_red_dark"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_purple"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_orange_dark"/>
</GridLayout>


效果图





修改一下方向

<GridLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:orientation="vertical">






发现问题

貌似在垂直(vertical)的时候行数(rowCount)才会起作用

而在水平(horizontal)的时候行数(columnCount)会起作用

<GridLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_blue_bright"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_columnSpan="2"
android:background="@android:color/holo_green_dark"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_red_dark"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_purple"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_orange_dark"/>
</GridLayout>




给第二个imageview设置了android:layout_columnSpan



因为只有两列所以就独占一行。其他的就被挤下去了

现在给第二个(深绿)imageview设置android:layout_rowSpan

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_rowSpan="2"
android:background="@android:color/holo_green_dark"/>






因为深绿的那个跨了两行,所以后面的也被挤下去了

android:layout_row和android:layout_column就不做介绍了,看描述就知道

GridLayout.Spec

GridLayout.Spec可以指定组件所在的行数和列数。

GridLayout.SpecrowSpec=GridLayout.spec(i/4+2);
GridLayout.SpeccolumnSpec=GridLayout.spec(i%4);
GridLayout.LayoutParamsparams=newGridLayout.LayoutParams(
rowSpec,columnSpec);
params.setGravity(Gravity.FILL|Gravity.BOTTOM);
layout.addView(bn,params);


网上抄的一段关于创建计算器界面的代码,可以看出使用了GridLayout.Spec来对组件进行排版

因为不怎么常用,所以就在这里简单提一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: