RelativeLayout常用属性总结

RelativeLayout常用属性

1、与兄弟控件的关系——方向位置

android:layout_above="@id/xxx"  --将控件置于给定ID控件之上

android:layout_below="@id/xxx"  --将控件置于给定ID控件之下

android:layout_toLeftOf="@id/xxx"  --将控件的右边缘和给定ID控件的左边缘对齐(将控件置于给定ID控件之左)

android:layout_toRightOf="@id/xxx"  --将控件的左边缘和给定ID控件的右边缘对齐(将控件置于给定ID控件之右)

2、与兄弟控件的关系——方向对齐

android:layout_alignLeft="@id/xxx"  --将控件的左边缘和给定ID控件的左边缘对齐

android:layout_alignTop="@id/xxx"  --将控件的上边缘和给定ID控件的上边缘对齐

android:layout_alignRight="@id/xxx"  --将控件的右边缘和给定ID控件的右边缘对齐

android:layout_alignBottom="@id/xxx"  --将控件的底边缘和给定ID控件的底边缘对齐

3、与父控件的关系——边缘对齐

android:layout_alignParentLeft="true"  --将控件的左边缘和父控件的左边缘对齐

android:layout_alignParentTop="true"  --将控件的上边缘和父控件的上边缘对齐

android:layout_alignParentRight="true"  --将控件的右边缘和父控件的右边缘对齐

android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐

4、与父控件的关系——居中对齐

android:layout_centerInParent="true"  --将控件置于父控件的中心位置

android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置

android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置

5、相对于基准线位置:

android:layout_alignBaseline=”@id/xxx” --以两控件字体下方的线对齐(针对英文)

6、头部尾部对齐(版本4.2以上的新属性)

android:layout_alignStart=”@id/xxx”  --将控件对齐给定ID控件的头部

android:layout_alignEnd=”@id/xxx”  --将控件对齐给定ID控件的尾部

android:layout_alignParentStart=true  --将控件对齐到父控件的头部

android:layout_alignParentEnd=true  --将控件对齐到父控件的尾部

 

7、通用属性

android:layout_margin=””  

android:layout_marginStart=”” 

android:layout_marginEnd=””  

android:layout_marginBottom=””  

android:layout_marginTop=””

android:layout_marginLeft=””

android:layout_marginRight=””

 

android:layout_padding=””  

android:layout_paddingStart=”” 

android:layout_paddingEnd=””  

android:layout_paddingBottom=””  

android:layout_paddingTop=””

android:layout_paddingLeft=””

android:layout_paddingRight=””

margin padding 填的都是 XXdp(或其他单位)

效果如下图

 

注:

  1. android:gravity= 用于控制在控件在水平方向上的位置
  2. 同时使用Android:layout_ alignParentLeft和Android:layout_alignParentRight可使控件拉长到匹配父控件,而不需要使用android:layout_width=”  ”这个属性来控制。
  3. 父控件指的是直接父控件,是父亲不是爷爷。
  4. 在android studio里用相对布局时,用到android:layout_toLeftOf时,会被提醒使用android:layout_alignStart更好。原因是,使用android:layout_alignStart为了本地化的原因,因为有些国家的文字是从右向左的。使用android:layout_alignStart就可以解决这种本地化的情况了。这个特性就是android从api17开始支持的RTL。margin_start 和 margin_end 也是同理。

 

参考:

http://www.cnblogs.com/defineconst/

https://www.cnblogs.com/chcong/p/3527455.html

http://blog.csdn.net/qq_32059827/article/details/51487997