android:layout_width=“match_parent” android:layout_height=“match_parent” android:orientation=“vertical”> android:id="@+id/ll_top" android:layout_width=“match_parent” android:layout_weight=“1” android:background="@color/color_188FFF" android:orientation=“horizontal” android:layout_height=“0dp”> android:id="@+id/ll_bottom" android:layout_width=“match_parent” android:layout_weight=“1” android:orientation=“horizontal” android:background="@color/color_ff0000" android:layout_height=“0dp”> 你也可创建线性布局,让子元素使用大小不同的屏幕空间: a、如果有三个文本字段,其中两个声明权重为 1,另一个未赋予权重,那么没有权重的第三个文本字段就不会展开,而仅占据其内容所需的区域。另一方面,另外两个文本字段将以同等幅度展开,填充测量三个字段后仍剩余的空间。 b、如果有三个文本字段,其中两个字段声明权重为 1,而为第三个字段赋予权重 2(而非 0),那么现在相当于声明第三个字段比另外两个字段更为重要,因此,该字段将获得总剩余空间的一半,而其他两个字段均享余下的空间。 android:layout_width=“match_parent” android:layout_height=“match_parent” android:orientation=“vertical”> android:id="@+id/ll_top" android:layout_width=“match_parent” android:layout_weight=“1” android:background="@color/color_188FFF" android:orientation=“horizontal” android:layout_height=“0dp”> android:id="@+id/view_top_1" android:layout_width=“100dp” android:layout_height=“match_parent” android:background="@color/color_ff0000"/> android:id="@+id/view_top_2" android:layout_width=“0dp” android:layout_height=“match_parent” android:layout_weight=“1” android:background="@color/purple_500"/> android:id="@+id/view_top_3" android:layout_width=“100dp” android:layout_height=“match_parent” android:background="@color/color_FF773D"/> android:id="@+id/ll_bottom" android:layout_width=“match_parent” android:layout_weight=“1” android:orientation=“horizontal” android:background="@color/color_ff0000" android:layout_height=“0dp”> android:id="@+id/view_bottom_1" android:layout_width=“0dp” android:layout_height=“match_parent” android:layout_weight=“1” android:background="@color/color_FF773D"/> android:id="@+id/view_bottom_2" android:layout_width=“0dp” android:layout_height=“match_parent” android:layout_weight=“2” android:background="@color/teal_200"/> android:id="@+id/view_bottom_3" android:layout_width=“0dp” android:layout_height=“match_parent” android:layout_weight=“1” android:background="@color/color_ff0000"/> 二、相对布局(RelativeLayout常用) RelativeLayout是一个视图组,每个子组件位置是相对的,可以相对于同一层级下其他控件,也可以相对于父控件。 RelativeLayout是用于设计用户界面的非常强大的实用程序,因为它可以消除嵌套视图组并保持布局层次结构平坦,从而提高性能。如果你发现自己使用了多个嵌套LinearLayout组,则可以将它们替换为单个RelativeLayout。 RelativeLayout让子视图指定它们相对于父视图或彼此的位置(由 ID 指定)。因此,如果A组件的位置是由B组件的位置来决定,Android要求先定义B组件,再定义A组件 XML属性 相关方法 说明 android:gravity setGravity(int) 指定对象应如何在其自身边界内在X轴和Y轴上定位其内容。 必须是Gravity常量值中的一个或多个(以“|”分隔)。 android:ignoreGravity setIgnoreGravity(int) 指示哪个视图不应受重力影响。 RelativeLayout.LayoutParams 为了控制RelativeLayout布局容器中各子组件的布局分布,RelativeLayout提供了一个内部类:RelativeLayout.LayoutParams,该类提供了大量的XML属性来控制RelativeLayout布局容器中子组件的布局分布。 RelativeLayout.LayoutParams里设的XML属性****及说明 XML属性 说明 android:layout_above 将此视图的底部边缘定位在给定的锚视图 ID 上方。 android:layout_alignbaseline 将此视图的基线定位在给定锚视图 ID 的基线上。 android:layout_alignBottom 使此视图的底部边缘与给定的锚视图 ID 的底部边缘匹配。 android:layout_alignEnd 使此视图的结束边缘与给定锚视图 ID 的结束边缘匹配。 android:layout_alignLeft 使此视图的左边缘与给定锚视图 ID 的左边缘匹配。 android:layout_alignParentBottom 如果为 true,则使此视图的底部边缘与父视图的底部边缘匹配。 android:layout_alignParentEnd 如果为 true,则使此视图的结束边缘与父视图的结束边缘匹配。 android:layout_alignParentLeft 如果为 true,则使此视图的左边缘与父视图的左边缘匹配。 android:layout_alignParentRight 如果为 true,则使此视图的右边缘与父视图的右边缘匹配。 android:layout_alignParentStart 如果为 true,则使此视图的起始边缘与父视图的起始边缘匹配。 android:layout_alignParentTop 如果为 true,则使此视图的顶部边缘与父视图的顶部边缘匹配。 android:layout_alignRight 使此视图的右边缘与给定锚视图 ID 的右边缘匹配。 android:layout_alignStart 使此视图的起始边缘与给定锚视图 ID 的起始边缘匹配。 android:layout_alignTop 使此视图的顶部边缘与给定的锚视图 ID 的顶部边缘匹配。 android:layout_alignWithParentIfMissing 如果设置为 true,则在 layout_toLeftOf、layout_toRightOf 等无法找到锚点时,将使用父级作为锚点。 android:layout_below 将此视图的顶部边缘定位在给定的锚视图 ID 下方。 android:layout_centerHorizontal 如果为 true,则此子项在其父项内水平居中。 android:layout_centerInParent 如果为 true,则此子项在其父项内水平和垂直居中。 android:layout_centerVertical 如果为 true,则将此子项垂直居中于其父项内。 android:layout_toEndOf 将此视图的起始边缘定位到给定锚视图 ID 的末尾。 android:layout_toLeftOf 将此视图的右边缘定位在给定锚视图 ID 的左侧。 android:layout_toRightOf 将此视图的左边缘定位在给定锚视图 ID 的右侧。 android:layout_toStartOf 将此视图的结束边缘定位到给定锚视图 ID 的开始处。 android:layout_above 将此视图的底部边缘定位在给定的锚视图 ID 上方。 除此之外,RelativeLayout.LayoutParams还继承了ViewGroup.MarginLayoutParams,因此RelativeLayout布局容器中每个子组件也可以指定ViewGroup.MarginLayoutParams所支持的各XML属性。 android:layout_width=“match_parent” android:layout_height=“match_parent”> android:id="@+id/view_top_1" android:layout_width=“match_parent” android:layout_height=“100dp” android:background="@color/color_666666"/> android:id="@+id/view_top_2" android:layout_width=“200dp” android:layout_height=“100dp” android:background="@color/teal_200"/> android:id="@+id/view_top_3" android:layout_width=“100dp” android:layout_height=“100dp” android:background="@color/color_FF773D"/> android:layout_width=“match_parent” android:layout_height=“match_parent”> android:id="@+id/view_top_1" android:layout_width=“match_parent” android:layout_height=“100dp” android:background="@color/color_666666"/> android:id="@+id/view_top_2" android:layout_width=“200dp” android:layout_below="@id/view_top_1" android:layout_height=“100dp” android:background="@color/teal_200"/> android:id="@+id/view_top_3" android:layout_width=“100dp” android:layout_below="@id/view_top_2" android:layout_height=“100dp” android:background="@color/color_FF773D"/> android:id="@+id/view_top_4" android:layout_width=“100dp” android:layout_below="@id/view_top_2" android:layout_toRightOf="@id/view_top_3" android:layout_height=“100dp” android:background="@color/purple_500"/> 三、网格布局(GridLayout) GridLayout把这个容器划分成rows×columns个网格,每个网格可以放一个组件。 除此之外,也可以设置一个组件横跨多少列、一个组件纵跨多少行(支持跨行和跨列以及每个单元格组内的任意对齐形式)。 GridLayout提供了setColumnCount(int)和setRowCount(int)方法来控制该网络的列数和行数。 XML属性 相关方法 说明 android:alignmentMode setAlignmentMode(int) 当设置为 alignMargins 时,会导致在视图的外边界之间进行对齐,由其边距定义。 android:columnCount setColumnCount(int) 自动定位子项时要创建的最大列数。 android:columnOrderPreserved setColumnOrderPreserved (boolean) 设置为 true 时,强制列边界以与列索引相同的顺序出现。 android:orientation setOrientation(int) 布局期间不使用方向属性。 android:rowCount setRowCount(int) 自动定位子项时要创建的最大行数。 android:rowOrderPreserved setRowOrderPreserved(boolean) 设置为 true 时,强制行边界以与行索引相同的顺序出现。 android:useDefaultMargins setUseDefaultMargins(boolean) 当设置为 true 时,告诉 GridLayout 在视图的布局参数中未指定任何边距时使用默认边距。 GridLayout**.LayoutParams** 为了控制GridLayout布局容器中各子组件的布局分布,GridLayout提供了一个内部类:GridLayout.LayoutParams,该类提供了大量的XML属性来控制GridLayout布局容器中子组件的布局分布。 GridLayout**.LayoutParams里设的XML属性和相关方法说明** XML属性 相关方法 说明 android:layout_column 界定此视图占用的单元格组左侧的列边界。 android:layout_columnSpan 列跨度:界定此视图占用的单元格组的左右边界之间的差异。 android:layout_columnWeight 在多余空间分配期间应分配给此视图的水平空间的相对比例。 android:layout_gravity setGravity(int) 重力指定组件应如何放置在其单元组中。 android:layout_row 界定此视图占据的单元格组顶部的行边界。 android:layout_rowSpan 行跨度:界定此视图占据的单元格组的顶部和底部边界之间的差异。 android:layout_rowWeight 在多余空间分配期间应分配给此视图的垂直空间的相对比例。 android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:columnCount=“4” android:rowCount=“5”>



