Android UI布局之RelativeLayout,androidlayout布局
RelativeLayout是一個相對布局類。首先RelativeLayout是一個容器,它裡邊的元素,如Button按鈕等的位置是按照相對位置來計算的,例如,有兩個Button按鈕都布局在一個RelativeLayout裡邊,我們可以定義第二個Button在第一個Button的上邊或者是右邊。但到底第二個Button在什麼位置呢,它還是依賴於第一個Button的位置。需要注意的是,出於效能上的考慮,對於相對布局的精確位置的計算只會執行一次,所以,如果一個可視化組件B依賴A,那麼必須要讓A出現在B的前邊。
執行個體:LayoutDemo
運行效果:
代碼清單:
布局檔案:relative_layout.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dip" > <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="請輸入使用者名稱:" /> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/label" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:text="取消" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/cancel" android:layout_alignTop="@id/cancel" android:text="確定" /></RelativeLayout>
Java原始碼檔案:RelativeLayoutActivity.java
package com.rainsong.layoutdemo;import android.app.Activity;import android.os.Bundle;public class RelativeLayoutActivity extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.relative_layout); }}
API知識點
android:id 定義組件的id
android:layout_width 定義組件的寬度
android:layout_height 定義組件的高度
android:padding 填充
android:layout_below 將當前組件放置於指定組件的下方
android:layout_alignParentRight 和父容器的右邊齊平
android:layout_marginLeft 右邊距
android:layout_toLeftOf 設定此組件在指定組件的左邊
android:layout_alignTop 設定此組件和指定組件高度齊平
安卓開發中的relativelayout布局中的問題
看你用什麼layout了,如果是LinearLayout的話是排列下方的控制項跟你的間距,如果是RelativeLayout的話你要指定對其方式,方式有很多種,例如android:layout_below和android:layout_above,或layout_alignParentRight或layout_toRightOf或layout_toLeftOf
對於android相對布局RelativeLayout
<
RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fd8d8d"
android:gravity="center" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="BUTTON"
android:textSize="16sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_toRightOf="@id/button"
android:text="BUTTON2"
android:textSize="16sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button"
android:layout_toRightOf="@id/button"
android:text="BUTTON1"
android:textSize="16sp" />
</
RelativeLayout>...餘下全文>>