歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:使用相對布局實現梅花圖案的實例

Android開發教程:使用相對布局實現梅花圖案的實例

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

各視圖組件按照相互之間的相對位置確定,表示將標簽中的元素放置到指定元素的下面是:

Android:layout_below=“@id/text”;將該元素放置在text元素下

Android:layout_ablove=“@id/text”; 將該元素放置在text元素上

Android:layout_toLeftOf=“@id/text”; 將該元素放置在text元素左面

Android:layout_alignTop=“@id/text”; 將該元素與text元素上部對齊

string.xml文件信息:

  1. <pre class="html" name="code"><string name="b00"></string>
  2. <string name="b11"></string>
  3. <string name="b22"></string>
  4. <string name="b33"></string>
  5. <string name="b44"></string>

Main.xml文件信息:

  1. <?xml version="1.0"encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <Button
  6. android:id="@+id/no.0"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_centerInParent="true"
  10. android:text="@string/b00"/>
  11. <Button
  12. android:id="@+id/no.1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_above="@id/no.0"
  16. android:layout_toLeftOf="@id/no.0"
  17. android:text="@string/b11"/>
  18. <Button
  19. android:id="@+id/no.2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_above="@id/no.0"
  23. android:layout_toRightOf="@id/no.0"
  24. android:text="@string/b22"/>
  25. <Button
  26. android:id="@+id/no.3"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_below="@id/no.0"
  30. android:layout_toLeftOf="@id/no.0"
  31. android:text="@string/b33"/>
  32. <Button
  33. android:id="@+id/no.4"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_below="@id/no.0"
  37. android:layout_toRightOf="@id/no.0"
  38. android:text="@string/b44"/>
  39. </RelativeLayout>

顯示效果圖:

整體思路:

首先在string.xml中定義文本的值,然後在main.xml中,定義整體布局,首先使用<RelativeLayout>標簽定義中間位置,然後以它的位置為中心,定義其他四個按鈕位置

Copyright © Linux教程網 All Rights Reserved