三、FrameLayout帧布局</strong></span> FrameLayout帧布局继承于ViewGroup组件,帧布局容器为每个加入其中的组件创建一个空白的区域(即为一帧)。每个子组件占据一帧,这些帧都会根据gravity属性执行自动对齐,帧布局的效果即把组件一个一个地叠加在一起。FrameLayout支持的常用XML属性如下:
(1)android:gravity:设置容器中所有子组件的对齐方式;
(2)android:layout_gravity:设置某个子组件相对于容器中的对齐方式;
(3)android:foreground:设置该帧布局容器的前景图像;
(4)android:foregroundGravity:定义绘制前景图像的gravity属性;
举例:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#f0f"
android:height="300dp"
android:width="300dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#f00"
android:height="250dp"
android:width="250dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ff0"
android:height="200dp"
android:width="200dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#aaa"
android:height="150dp"
android:width="150dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#fc0"
android:height="100dp"
android:width="100dp" />
</FrameLayout>
显示效果:
注:android:width 定义控件上面的文本(TextView) 的宽度,需要与 android:layout_width 配合起来作用的,如果 android:layout_width="fill_parent" 的话,那么设置 android:width 是没有意义的。
四、RelativeLayout相对布局</strong></span> RelativeLayout表示相对布局,相对布局管理器内子组件的位置总是相对兄弟组件、父容器来决定,每个组件或容器事先需指定android:id属性。RelativeLayout相对布局支持的常用XML属性如下:
(1)android:gravity:设置该布局容器各子组件的对齐方式(整体相对于父容器);
(2)android:ignoreGravity:设置哪个组件不受gravity属性的影响;
(3)android:layout_centerHorizontal:控制该子组件是否位于布局容器的水平居中;
(4)android:layout_centerVertical:控制该子组件是否位于布局容器的垂直居中;
(5)android:layout_centerInParent:控制该子组件是否位于布局容器的中央位置;
(6)android:layout_alignParentButtom:控制该子组件是否位于布局容器的低端对齐;
(7)android:layout_alignParentLeft:控制该子组件是否位于布局容器的左边对齐; (8)android:layout_alignParentRight:控制该子组件是否位于布局容器的右边对齐;
(9)android:layout_alignParentTop:控制该子组件是否位于布局容器的顶端对齐;
(10)android:layout_toRightOf:控制该子组件位于给出ID组件的右侧;
(11)android:layout_toLeftOf:控制该子组件位于给出ID组件的左侧;
(12)android:layout_above:控制该子组件位于给出ID组件的上方;
(13)android:layout_below:控制该子组件位于给出ID组件的下方;
(14)android:layout_alignTop:控制该子组件位于给出ID组件的上边界对齐;
(15)android:layout_alignBottom:控制该子组件位于给出ID组件的下边界对齐;
(16)android:layout_alignLeft:控制该子组件位于给出ID组件的左边界对齐;
(17)android:layout_alignRight:控制该子组件位于给出ID组件的右边界对齐;
</div>
五、GridLayout网格布局</strong></span> GridLayout网格布局是Android 4.0新增的布局管理器,只能在Android 4.0及以后的版本使用。GridLayout类似于HTML中的table标签,它把整个容器划分成为"行*列"个网格,每个网格可以放置一个组件,另外,也可以设置一个组件横跨多个列和多个行。GridLayout相对布局支持的常用XML属性如下:
(1)android:alignmentMode:设置该布局管理器采用的对齐模式;
(2)android:columnCount:设置该网格的列数量;
(3)android:clolumnOrderPreserved:设置该网格容器是否保留列序列号;
(4)android:rowCount:设置该网格的行数量;
(5)android:rowOrderPreserved:设置该网格容器是否保留行序列号;
(6)android:useDefaultMargins</span>:设置该布局管理器是否使用默认的页边距</span> </div> (7)android:layout_column:设置该子组件在GridLayout的第几列
(8)android:layout_columnSpan:设置该子组件在GridLayout横向上跨几行列
(9)android:layout_row:设置该子组件在GridLayout的第几行
(10)android:layout_rowSpan:设置该子组件在GridLayout纵向上跨几行
(11)android:layout_gravity:设置该子组件采用何种方式占据该网格的空间(相对于单个网格而言)
六、Android中常用的距离单位
1.px:像素,即每个px对应屏幕上的一个店。
2.dip/dp:设备独立像素。即一种基于屏幕密度的抽象单位,在每英寸160点的显示器上,1dip=1px,用于设置组件大小或距离。随着屏幕密度的改变,dip与px的换算会发生改变。
3.sp(scaled pixels,比例像素):主要处理字体的大小,可以根据用户的字体大小首选项进行缩放;
4.in:英寸,标准的长度单位;
5.pt:磅,1/72英寸