常用布局
- padding:内边距
- android:paddingLeft=”30dp” 控件的内容距离控件左边界30dp
- layout_margin:外边距
- android:layout_marginRight=”30dp” 此控件距离右边控件(或父布局)30dp , (使用这个的时候一般要同时使用android:layout_alignParentRight=”true”)
- layout_gravity:相对其父布局的位置(center,bottom,right…)
- gravity:控件中内容的位置(center,bottom,right…)
线性布局
- LinearLayout
指定各个节点的排列方向
android:orientation="horizontal"
- 该布局内控件的layout_gravity属性
- 如果是水平排列:不能设置水平方向上的位置:比如right,center_horizontal
- 如果是垂直排列:不能设置垂直方向上的位置:比如bottom,center_vertical
线性布局非常重要的一个属性:权重(按比例分配屏幕的剩余宽度或者高度)
android:layout_weight="1"
- 要设置横向或纵向权重,则需将width或height的值设置为”0dp”
相对布局
- RelativeLayout
- 控件的位置
- android:layout_above=”@id/xxx” –将控件置于给定ID控件之上
- android:layout_below=”@id/xxx” –将控件置于给定ID控件之下
- android:layout_toLeftOf=”@id/xxx” –将控件置于给定ID控件之左
- android:layout_toRightOf=”@id/xxx” –将控件置于给定ID控件之右
- android:layout_alignParentLeft=”true” –将控件的左边缘和父控件的左边缘对齐
- android:layout_alignParentTop=”true” –将控件的上边缘和父控件的上边缘对齐
- android:layout_alignParentRight=”true” –将控件的右边缘和父控件的右边缘对齐
- android:layout_alignParentBottom=”true” –将控件的底边缘和父控件的底边缘对齐
- android:layout_centerInParent=”true” –将控件置于父控件的中心位置
- android:layout_centerHorizontal=”true” –将控件置于水平方向的中心位置
- android:layout_centerVertical=”true” –将控件置于垂直方向的中心位置
- 为了保证印刷字母的整齐而划定的线 baseline
- android:layout_alignBaseline=”@id/xxx” 让两个控件的内容的基准线对齐
- 控件和控件之间的对齐方式:@id/控件id
- android:layout_alignLeft=”@id/xxx” –将控件的左边缘和给定ID控件的左边缘对齐
- android:layout_alignTop=”@id/xxx” –将控件的上边缘和给定ID控件的上边缘对齐
- android:layout_alignRight=”@id/xxx” –将控件的右边缘和给定ID控件的右边缘对齐
- android:layout_alignBottom=”@id/xxx” –将控件的底边缘和给定ID控件的底边缘对齐
帧布局
- FrameLayout
- 示例工程名 : FrameLayoutOnListView
- 所有控件默认放置于左上角,若有多个控件则会覆盖
- 控件可以使用layout_gravity属性制定控件的对齐方式
- 前景图像:永远处于帧布局最顶端,直接面对用户的图像,不会被覆盖
- android:foreground:设置该帧布局容器的前景图像
- android:foregroundGravity:设置前景图像显示的位置
- 帧布局在游戏开发方面用的比较多,用于显示自定义的视图,达到分层的效果。
表格布局
- TableLayout
- 每个节点是一行,它的每个子节点是一列
- 表格布局中的节点可以不设置宽高,因为设置了也无效
- 根节点的子节点宽为匹配父元素,高为包裹内容
- 节点的子节点宽为包裹内容,高为包裹内容
- 以上默认属性无法修改
根节点中可以设置以下属性,表示让第1列拉伸填满屏幕宽度的剩余空间
android:stretchColumns="1"