Android布局容器
无论是普通视图还是布局容器都继承自View,其中ViewGroup就是所有布局的父类,ViewGroup继承自View同时可以对View进行管理 。
不同布局管理器的共性
//容器的宽
android:layout_width
//容器的高
android:layout_height
//容器内部对齐方式:left/right/top/bottom/center/
//center_horizontal/center_vertical
android:gravity
//容器的背景,可以是颜色或图片
android:background
//容器的内边距,同时还可以根据情况有选择性的设置内边距,paddingTop...
android:padding
//容器的外边距,同时还可以根据情况有选择性的设置外边距,layout_marginLeft...
android:layout_margin
//容器的可见性,visible(可见)/invisible(不可见占位)/gone(不可见不占位)
android:visibility
不同布局管理器的特性
1、帧布局
FrameLayout,布局特性是所有孩子默认叠在该容器左上角。
特色属性:android:layout_gravity
作用:子控件用来调整自己在父容器中的位置
2、线性布局
LinearLayout,可以水平编排或者垂直编排子控件的显示。
特色属性:
//排列方向,vertical垂直(沿着y坐标),horizontal水平方向(沿着x坐标)
android:orientation
//权重,将LinearLayout中剩下的部分进行比例划分
android:layout_weight
//占位视图Space,通过权重占位效果很好
<Space
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1" />
3、相对布局
RelativeLayout,根据参考控件的相对位置完成定位。
布局容器作为参考:
//相对位置
android:layout_alignParentLeft //靠父容器左侧
android:layout_alignParentRight //靠父容器右侧
android:layout_alignParentTop //靠父容器顶部
android:layout_alignParentBottom //靠父容器底部
//对齐方式
android:layout_centerVertical //垂直居中
android:layout_centerInParent //容器正中
android:layout_centerHorizontal //水平居中
子控件作为参考:
//相对位置
android:layout_toLeftOf //参照物的左边
android:layout_toRightOf //参照物的右边
android:layout_above //参照物的上面
android:layout_below //参照物的下面
//对齐方式
android:layout_alignTop //与参照物顶部对齐
android:layout_alignLeft //与参照物左对齐
android:layout_alignRight //与参照物右对齐
android:layout_alignBottom //与参照物底部对齐