现在的位置: 首页 > 综合 > 正文

关于相对布局RelativeLayout

2013年12月02日 ⁄ 综合 ⁄ 共 4802字 ⁄ 字号 评论关闭
RelativeLayout
相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。
下面我们用相对布局再做一次上面的例子,首先放置一个图片,其它两个文本分别相对上一个元素定位:

  1. <?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout android:id=”@+id/RelativeLayout01″
  2. android:layout_width=”fill_parent”
  3. android:layout_height=”fill_parent”
  4. android:background=”#fff”
  5. xmlns:android=”http://schemas.android.com/apk/res/android”><ImageView android:id=”@+id/ImageView01″
  6. android:src=”@drawable/android”
  7. android:layout_width=”fill_parent”
  8. android:layout_height=”wrap_content”
  9. android:layout_marginTop=”40dip”
  10. >
  11. </ImageView>
  12. <TextView
  13. android:layout_height=”wrap_content”
  14. android:layout_width=”wrap_content”
  15. android:id=”@+id/TextView01″
  16. android:text=”Android2.2 学习指南”
  17. android:textColor=”#0f0″
  18. android:textSize=”28dip”
  19. android:layout_below=”@id/ImageView01″
  20. android:layout_centerHorizontal=”true”
  21. android:layout_marginTop=”10dip”>
  22. </TextView>
  23. <TextView
  24. android:layout_height=”wrap_content”
  25. android:layout_width=”wrap_content”
  26. android:id=”@+id/TextView02″
  27. android:text=”图文并茂,理论清晰,操作性强”
  28. android:textColor=”#333″
  29. android:textSize=”18dip”
  30. android:layout_below=”@id/TextView01″
  31. android:layout_centerHorizontal=”true”
  32. android:layout_marginTop=”5dip“>
  33. </TextView>
  34. </RelativeLayout>
  35. 从上图可以看到界面效果基本保持了一致,而不是像绝对定位一样龟缩在左上角;同学们看到自动缩放的功能是采用了dip做单位带来的好处。关于dip,不懂的同学可以看我在开发小知识里写的专门的文章。
    下面介绍一下RelativeLayout用到的一些重要的属性:
    第一类:属性值为true或false

    android:layout_centerHrizontal                                           水平居中

    android:layout_centerVertical                                            垂直居中

    android:layout_centerInparent                                           相对于父元素完全居中

    android:layout_alignParentBottom                                     贴紧父元素的下边缘

    android:layout_alignParentLeft                                          贴紧父元素的左边缘

    android:layout_alignParentRight                                        贴紧父元素的右边缘

    android:layout_alignParentTop                                          贴紧父元素的上边缘

    android:layout_alignWithParentIfMissing                            如果对应的兄弟元素找不到的话就以父元素做参照物
    第二类:属性值必须为id的引用名“@id/id-name”

    android:layout_below                          在某元素的下方

    android:layout_above                          在某元素的的上方

    android:layout_toLeftOf                       在某元素的左边

    android:layout_toRightOf                     在某元素的右边
    android:layout_alignTop                      本元素的上边缘和某元素的的上边缘对齐

    android:layout_alignLeft                      本元素的左边缘和某元素的的左边缘对齐

    android:layout_alignBottom                 本元素的下边缘和某元素的的下边缘对齐

    android:layout_alignRight                    本元素的右边缘和某元素的的右边缘对齐
    第三类:属性值为具体的像素值,如30dip,40px

    android:layout_marginBottom              离某元素底边缘的距离

    android:layout_marginLeft                   离某元素左边缘的距离

    android:layout_marginRight                 离某元素右边缘的距离

    android:layout_marginTop                   离某元素上边缘的距离
    我们再把上面的例子重新做一遍,这一次多放一些属性在里面,大家试验一下:
    1. <?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout android:id=”@+id/RelativeLayout01″
    2. android:layout_width=”fill_parent”
    3. android:layout_height=”fill_parent”
    4. android:background=”#cfff” 色彩的设置是argb,第一个c是透明度
    5. xmlns:android=”http://schemas.android.com/apk/res/android”><ImageView android:id=”@+id/ImageView01″
    6. android:src=”@drawable/android”
    7. android:layout_width=”wrap_content”
    8. android:layout_height=”wrap_content”
    9. android:layout_marginTop=”40dip”
    10. android:layout_centerHorizontal=”true”>
    11. </ImageView><TextView
    12. android:layout_height=”wrap_content”
    13. android:layout_width=”wrap_content”
    14. android:id=”@+id/TextView01″
    15. android:text=”Android2.2 学习指南”
    16. android:textColor=”#0f0″
    17. android:textSize=”28dip”
    18. android:layout_below=”@id/ImageView01″
    19. android:layout_centerHorizontal=”true”
    20. android:layout_marginTop=”10dip”>
    21. </TextView><TextView
    22. android:layout_height=”wrap_content”
    23. android:layout_width=”wrap_content”
    24. android:id=”@+id/TextView02″
    25. android:text=”图文并茂,理论清晰,操作性强”
    26. android:textColor=”#333″
    27. android:textSize=”18dip”
    28. android:layout_below=”@id/TextView01″
    29. android:layout_centerHorizontal=”true”
    30. android:layout_marginTop=”5dip”>
    31. </TextView><TextView
    32. android:layout_height=”wrap_content”
    33. android:layout_width=”wrap_content”
    34. android:id=”@+id/TextView03″
    35. android:text=”alignTop”
    36. android:textColor=”#333″
    37. android:textSize=”18dip”
    38. android:layout_alignTop=”@id/ImageView01″  和ImageView01上边缘对齐
    39. android:layout_centerHorizontal=”true”>
    40. </TextView><TextView
    41. android:layout_height=”wrap_content”
    42. android:layout_width=”wrap_content”
    43. android:id=”@+id/TextView04″
    44. android:text=”alignLeft”
    45. android:textColor=”#333″
    46. android:textSize=”18dip”
    47. android:layout_alignLeft=”@id/ImageView01″
    48. android:layout_centerHorizontal=”true”>
    49. </TextView><TextView
    50. android:layout_height=”wrap_content”
    51. android:layout_width=”wrap_content”
    52. android:id=”@+id/TextView05″
    53. android:text=”alignRight”
    54. android:textColor=”#333″
    55. android:textSize=”18dip”
    56. android:layout_alignRight=”@id/ImageView01″
    57. android:layout_centerHorizontal=”true”>
    58. </TextView><TextView
    59. android:layout_height=”wrap_content”
    60. android:layout_width=”wrap_content”
    61. android:id=”@+id/TextView06″
    62. android:text=”alignBottom”
    63. android:textColor=”#333″
    64. android:textSize=”18dip”
    65. android:layout_alignBottom=”@id/ImageView01″
    66. android:layout_centerHorizontal=”true”>
    67. </TextView>
    68. </RelativeLayout>

【上篇】
【下篇】

抱歉!评论已关闭.