各視圖組件按照相互之間的相對位置確定,表示將標籤中的元素放置到指定元素的下面是:
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文件信息:
<string name="b00">根</string> <string name="b11">莖</string> <string name="b22">花</string> <string name="b33">皮</string> <string name="b44">葉</string>
Main.xml文件信息:
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/no.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/b00"/>
<Button
android:id="@+id/no.1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/no.0"
android:layout_toLeftOf="@id/no.0"
android:text="@string/b11"/>
<Button
android:id="@+id/no.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/no.0"
android:layout_toRightOf="@id/no.0"
android:text="@string/b22"/>
<Button
android:id="@+id/no.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/no.0"
android:layout_toLeftOf="@id/no.0"
android:text="@string/b33"/>
<Button
android:id="@+id/no.4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/no.0"
android:layout_toRightOf="@id/no.0"
android:text="@string/b44"/>
</RelativeLayout>
顯示效果圖:
整體思路:
首先在string.xml中定義文本的值,然後在main.xml中,定義整體佈局,首先使用<RelativeLayout>標籤定義中間位置,然後以它的位置爲中心,定義其他四個按鈕位置