Android开发教程:使用相对布局实现梅花图案的实例


各视图组件按照相互之间的相对位置确定,表示将标签中的元素放置到指定元素的下面是:

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.   
  3.             <string name="b11"></string>  
  4.   
  5.             <string name="b22"></string>  
  6.   
  7.             <string name="b33"></string>  
  8.   
  9.             <string name="b44"></string>  

Main.xml文件信息:

  1. <?xml version="1.0"encoding="utf-8"?>  
  2.   
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:layout_width="match_parent"  
  6.   
  7.     android:layout_height="match_parent">  
  8.   
  9.    
  10.   
  11.     <Button  
  12.   
  13.         android:id="@+id/no.0"  
  14.   
  15.         android:layout_width="wrap_content"  
  16.   
  17.         android:layout_height="wrap_content"  
  18.   
  19.         android:layout_centerInParent="true"  
  20.   
  21.         android:text="@string/b00"/>  
  22.   
  23.    
  24.   
  25.     <Button  
  26.   
  27.         android:id="@+id/no.1"  
  28.   
  29.         android:layout_width="wrap_content"  
  30.   
  31.         android:layout_height="wrap_content"  
  32.   
  33.         android:layout_above="@id/no.0"  
  34.   
  35.         android:layout_toLeftOf="@id/no.0"  
  36.   
  37.         android:text="@string/b11"/>  
  38.   
  39.    
  40.   
  41.     <Button  
  42.   
  43.         android:id="@+id/no.2"  
  44.   
  45.         android:layout_width="wrap_content"  
  46.   
  47.         android:layout_height="wrap_content"  
  48.   
  49.         android:layout_above="@id/no.0"  
  50.   
  51.         android:layout_toRightOf="@id/no.0"  
  52.   
  53.         android:text="@string/b22"/>  
  54.   
  55.    
  56.   
  57.     <Button  
  58.   
  59.         android:id="@+id/no.3"  
  60.   
  61.         android:layout_width="wrap_content"  
  62.   
  63.         android:layout_height="wrap_content"  
  64.   
  65.         android:layout_below="@id/no.0"  
  66.   
  67.         android:layout_toLeftOf="@id/no.0"  
  68.   
  69.         android:text="@string/b33"/>  
  70.   
  71.    
  72.   
  73.     <Button  
  74.   
  75.         android:id="@+id/no.4"  
  76.   
  77.         android:layout_width="wrap_content"  
  78.   
  79.         android:layout_height="wrap_content"  
  80.   
  81.         android:layout_below="@id/no.0"  
  82.   
  83.         android:layout_toRightOf="@id/no.0"  
  84.   
  85.         android:text="@string/b44"/>  
  86.   
  87.    
  88.   
  89. </RelativeLayout>  

显示效果图:

整体思路:

首先在string.xml中定义文本的值,然后在main.xml中,定义整体布局,首先使用<RelativeLayout>标签定义中间位置,然后以它的位置为中心,定义其他四个按钮位置

相关内容