网格布局默认从左往右、从上到下排列,它先从第一行从左往右放置下级视图,塞满之后另起一行放置其余的下级视图,如此循环往复直至所有下级视图都放置完毕。为了判断能够容纳几行几列,网格布局新增了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>

效果:

android的网格布局 android网格布局滚动_滚动视图

 

 

滚动视图也分为垂直方向和水平方向两类,其中垂直滚动视图名为ScrollView,水平滚动视图名为HorizontalScrollView。

(1)垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。 

(2)水平方向滚动时,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent。 

(3)滚动视图节点下面必须且只能挂着一个子布局节点,否则会在运行时报错Caused by:java.lang.IllegalStateException:ScrollView can host only one direct child。 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- HorizontalScrollView是水平方向的滚动视图,当前高度为200dp -->
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="200dp">
        <!-- 水平方向的线性布局,两个子视图的颜色分别为青色和黄色 -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <View
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#aaffff" />
            <View
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#ffff00" />
        </LinearLayout>
    </HorizontalScrollView>
    <!-- ScrollView是垂直方向的滚动视图,当前高度为自适应 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- 垂直方向的线性布局,两个子视图的颜色分别为绿色和橙色 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#00ff00" />
            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#ffffaa" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

ScrollView在纵向滚动,而HorizontalScrollView在横向滚动。有时ScrollView的实际内容不够,又想让它充满屏幕,如果把layout_height属性赋值为match_parent,结果还是不会充满,正确的做法是再增加一行属性android:fillViewport(该属性为true表示允许填满视图窗口),属性片段举例如下:

android:layout_height="match_parent"
android:fillViewport="true"