Android布局(二)--- RelativeLayout(相对布局)

当界面比较复杂的时候,嵌套多层的 LinearLayout会降低UI Render的效率(渲染速度),而且在listview或者GridView上效率会更低,太多层的LinearLayout嵌套也会占用更多的系统资源,还有可能引发stackoverflow。如果使用RelativeLayout的话,则可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margin +padding就可以设置组件的显示位置。总结:使用RelativeLayout + LinearLayout的weight属性搭配使用可以适用于绝大多数的布局开发。

Android布局(二)--- RelativeLayout(相对布局)_第1张图片

根据父容器定位属性示意图

Android布局(二)--- RelativeLayout(相对布局)_第2张图片

根据兄弟组件定位(兄弟组件就是处于同一层次容器的组件)

Android布局(二)--- RelativeLayout(相对布局)_第3张图片

如果在相对布局里,控件没有指明相对位置,则默认都是在相对布局的左上角。

1、基本属性:

android:gravity 容器内组件的对齐方式

android:ignoreGravity 设置了该属性为true的组件将不受gravity的影响

2、根据父容器定位的属性:(值为true或false)

android:layout_alignParentLeft  左对齐

android:layout_alignParentRight  右对齐

android:layout_alignParentTop  顶部对齐

android:layout_alignParentBottom  底部对齐

android:layout_centerHorizontal  水平居中

android:layout_centerVertical  垂直居中

android:layout_centerInParent  位于父容器中心

3、根据兄弟组件定位的属性:(属性值必须为id的引用名“@id/id-name”)

android:layout_toLeftOf  参考组件的左边

android:layout_toRightOf  参考组件的右边

android:layout_above  参考组件的上方

android:layout_below  参考组件的下方

android:layout_alignTop  对齐参考组件的上边界

android:layout_alignBottom  对齐参考组件的下边界

android:layout_alignLeft  对齐参考组件的左边界

android:layout_alignRight  对齐参考组件的右边界

4、设置组件与父容器的边距(偏移),属性值为具体的像素值

android:layout_margin  设置组件离上下左右的偏移量

android:layout_marginLeft  设置组件离左边的偏移量

android:layout_marginRight  设置组件离右边的偏移量

android:layout_marginTop  设置组件离上边的偏移量

android:layout_marginBottom  设置组件离下边的偏移量

5、设置组件内部元素间的边距(属性值为具体的像素值)

android:padding  往内部元素上下左右填充一定边距

android:paddingLeft  往内部元素的左边填充一定边距

android:paddingRight  往内部元素的右边填充一定边距

android:paddingTop  往内部元素的上边填充一定边距

android:paddingBottom  往内部元素的下边填充一定边距

你可能感兴趣的:(Android布局(二)--- RelativeLayout(相对布局))