歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之RelativeLayout (相對布局)+梅花效果案例

Android應用開發之RelativeLayout (相對布局)+梅花效果案例

日期:2017/3/1 10:49:25   编辑:Linux編程

相對布局中的視圖組件是按相互之間的相對位置來確定的, 並不是線性布局中的必須

按行或按列單個顯示。示例布局文件如下:

main.xml

  1. <? xml version = "1.0"encoding = "utf-8" ?>
  2. < RelativeLayout
  3. xmlns:Android ="http://schemas.android.com/apk/res/android"
  4. android:layout_width ="fill_parent"
  5. android:layout_height = "fill_parent"
  6. >
  7. < TextView
  8. android:layout_width ="fill_parent"
  9. android:layout_height ="wrap_content"
  10. android:text ="@string/name_text"
  11. android:id = "@+id/text" />
  12. < EditText
  13. android:layout_width ="fill_parent"
  14. android:layout_height ="wrap_content"
  15. android:layout_below = "@id/text"
  16. android:id = "@+id/edit" />
  17. < Button
  18. android:layout_width ="wrap_content"
  19. android:layout_height ="wrap_content"
  20. android:text ="@string/cancle_button"
  21. android:layout_alignParentRight ="true"
  22. android:layout_below = "@id/edit"
  23. android:id = "@+id/cancle" />
  24. < Button
  25. android:layout_width ="wrap_content"
  26. android:layout_height ="wrap_content"
  27. android:layout_toLeftOf ="@id/cancle"
  28. android:layout_alignTop ="@id/cancle"
  29. android:text ="@string/ok_button" />
  30. </ RelativeLayout >

說明:

android:layout_below="@id/text" :將該元素放到 id 為 text 的元素的下面

android:layout_toLeftOf="@id/ok" :放到 id 為 ok 的元素左邊

android:layout_alignTop="@id/ok" :對齊 id 為 ok 的元素的頂部

還有很多關於相對位置的字段,希望大家能夠自己去發現

界面效果如圖:


案例二:梅花效果

問題:利用相對布局實現下面的效果


案例分析:

我們可以從途中看出,四周的方框的角都與中間的方框相連,而且呈現出X字樣.試想,中間的是不是有什麼特殊含義?

Copyright © Linux教程網 All Rights Reserved