网格布局GridLayout

虽然线性布局既能在水平方向排列,也能在垂直方向排列,但它不支持多行多列的布局方式,只支持单
行(水平排列)或单列(垂直排列)的布局方式。若要实现类似表格那样的多行多列形式,可采用网格
布局GridLayout。
网格布局默认从左往右、从上到下排列,它先从第一行从左往右放置下级视图,塞满之后另起一行放置
其余的下级视图,如此循环往复直至所有下级视图都放置完毕。为了判断能够容纳几行几列,网格布局
新增了android:columnCount与android:rowCount两个属性,其中columnCount指定了网格的列数,
即每行能放多少个视图;rowCount指定了网格的行数,即每列能放多少个视图

<!-- 根布局为两行两列的网格布局,其中列数由columnCount指定,行数由rowCount指定 -->
 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:columnCount="2"
 android:rowCount="2">
 <TextView
 android:layout_width="180dp"
 android:layout_height="60dp"
 android:gravity="center"
 android:background="#ffcccc"
 android:text="浅红色"
 android:textColor="#000000"
 android:textSize="17sp" />
 <TextView
 android:layout_width="180dp"
 android:layout_height="60dp"
 android:gravity="center"
 android:background="#ffaa00"
 android:text="橙色"
android:textColor="#000000"
 android:textSize="17sp" />
 <TextView
 android:layout_width="180dp"
 android:layout_height="60dp"
 android:gravity="center"
 android:background="#00ff00"
 android:text="绿色"
 android:textColor="#000000"
 android:textSize="17sp" />
 <TextView
 android:layout_width="180dp"
 android:layout_height="60dp"
 android:gravity="center"
 android:background="#660066"
 android:text="深紫色"
 android:textColor="#000000"
 android:textSize="17sp" />
 </GridLayout>

 

posted on 2024-12-04 14:27  leapss  阅读(14)  评论(0)    收藏  举报