1. 程式人生 > >android 使用程式碼實現 RelativeLayout佈局!

android 使用程式碼實現 RelativeLayout佈局!

使用 AbsoluteLayout 可以直接指定其子 View 的絕對位置, 這種佈局方式雖然簡單,但是不夠靈活。比如在一個程式中,按鈕2 位於 按鈕1 的下方且和 按鈕1 左對齊,我們可以使用指定兩個按鈕的絕對位置的方式佈局,但是當佈局完成後,由於某些原因,這兩個按鈕需要相左平移一些距離以便在父 View 右邊留出一些空白區域,那麼我們就需要同時修改 按鈕1 和 按鈕2 的 layout params。如果佈局更復雜一些呢?這樣“牽一髮而動全身”的佈局模式恐怕不是那麼友好吧?

  RelativeLayout,顧名思義,就是以“相對”位置/對齊 為基礎的佈局方式。android.widget.RelativeLayout 有個 繼承自android.view.ViewGroup.LayoutParams 的內嵌類 LayoutParams,使用這個類的例項呼叫 RelativeLayout.addView 就可以實現“相對佈局”。

  android.widget.RelativeLayout.LayoutParams 有一個建構函式:RelativeLayout.LayoutParams(int w, int h),引數指定了子 View 的寬度和高度,這一點和其父類是一樣的。而實現相對佈局的關鍵在它的 兩個 addRule 方法上。anchor 引數指定可以是 View 的 id(“相對於誰”)、RelativeLayout.TRUE(啟用某種對齊方式) 或者 是-1(應用於某些不需要 anchor 的 verb);AddRule 方法的 verb 引數指定相對的“動作”(以下常量均定義於 android.widget.RelativeLayout中,為了簡便不給出其全名):

  ALIGN_BOTTOM、ALIGN_LEFT、 ALIGN_RIGHT、 ALIGN_TOP: 本 View 的 底邊/左邊/右邊/頂邊 和 anchor 指定的 View 的 底邊/左邊/右邊/頂邊 對齊。
ALIGN_WITH_PARENT_BOTTOM 、ALIGN_WITH_PARENT_LEFT 、 ALIGN_WITH_PARENT_RIGHT 、 ALIGN_WITH_PARENT_TOP : 和上面一組常量類似,只不過不需要再指定 anchor, 其 anchor 自動為 Parent View。
CENTER_HORIZONTAL、CENTER_IN_PARENT 、CENTER_VERTICAL : 如果 anchor 為 TRUE,在 Parent 中 水平居中/水平和垂直均居中/垂直居中。
POSITION_ABOVE 、POSITION_BELOW 、 POSITION_TO_LEFT 、POSITION_TO_RIGHT : 本 View 位於 anchor 指定的 View 的 上邊/下邊/左邊/右邊。

  看一個例子:

package com.farproc.RLTest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;

public class RLTest extends Activity {
   
    private RelativeLayout rl;
   
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
   
    private static final int ID_BTN1 = 1;
    private static final int ID_BTN2 = 2;
    private static final int ID_BTN3 = 3;
    private static final int ID_BTN4 = 4;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
       
        rl = new RelativeLayout(this);
       
        btn1 = new Button(this);
        btn1.setText("----------------------");
        btn1.setId(ID_BTN1);
        
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp1.addRule(RelativeLayout.ALIGN_WITH_PARENT_TOP);
        lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        // btn1 位於父 View 的頂部,在父 View 中水平居中
        rl.addView(btn1, lp1 );
       
        btn2 = new Button(this);
        btn2.setText("|\n|\n|\n|\n|\n|");
        btn2.setId(ID_BTN2);
       
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp2.addRule(RelativeLayout.POSITION_BELOW, ID_BTN1);
        lp2.addRule(RelativeLayout.ALIGN_LEFT, ID_BTN1);
        // btn2 位於 btn1 的下方、其左邊和 btn1 的左邊對齊
        rl.addView(btn2, lp2);
       
        btn3 = new Button(this);
        btn3.setText("|\n|\n|\n|\n|\n|");
        btn3.setId(ID_BTN3);
       
        RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       lp3.addRule(RelativeLayout.POSITION_BELOW, ID_BTN1);
        lp3.addRule(RelativeLayout.POSITION_TO_RIGHT, ID_BTN2);
        lp3.addRule(RelativeLayout.ALIGN_RIGHT, ID_BTN1);
        // btn3 位於 btn1 的下方、btn2 的右方且其右邊和 btn1 的右邊對齊(要擴充)
        rl.addView(btn3,lp3);
       
        btn4 = new Button(this);
        btn4.setText("--------------------------------------------");
        btn4.setId(ID_BTN4);
       
        RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp4.addRule(RelativeLayout.POSITION_BELOW, ID_BTN2);
        lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        // btn4 位於 btn2 的下方,在父 Veiw 中水平居中
        rl.addView(btn4,lp4);
       
       
        setContentView(rl);
    }
}

轉自:
http://hi.baidu.com/xiechengfa/blog/item/5a3570eb3afb1a31b90e2d54.html