歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Layout 之 RelativeLayout RelativeLayout.LayoutParams

Android Layout 之 RelativeLayout RelativeLayout.LayoutParams

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

使用 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 的 上邊/下邊/左邊/右邊。

看一個例子:

  1. package com.farproc.RLTest;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.*;
  5. import android.view.*;
  6. public class RLTest extends Activity {
  7. private RelativeLayout rl;
  8. private Button btn1;
  9. private Button btn2;
  10. private Button btn3;
  11. private Button btn4;
  12. private static final int ID_BTN1 = 1;
  13. private static final int ID_BTN2 = 2;
  14. private static final int ID_BTN3 = 3;
  15. private static final int ID_BTN4 = 4;
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle icicle) {
  19. super.onCreate(icicle);
  20. rl = new RelativeLayout(this);
  21. btn1 = new Button(this);
  22. btn1.setText("----------------------");
  23. btn1.setId(ID_BTN1);
  24. RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  25. lp1.addRule(RelativeLayout.ALIGN_WITH_PARENT_TOP);
  26. lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
  27. // btn1 位於父 View 的頂部,在父 View 中水平居中
  28. rl.addView(btn1, lp1 );
  29. btn2 = new Button(this);
  30. btn2.setText("|\n|\n|\n|\n|\n|");
  31. btn2.setId(ID_BTN2);
  32. RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  33. lp2.addRule(RelativeLayout.POSITION_BELOW, ID_BTN1);
  34. lp2.addRule(RelativeLayout.ALIGN_LEFT, ID_BTN1);
  35. // btn2 位於 btn1 的下方、其左邊和 btn1 的左邊對齊
  36. rl.addView(btn2, lp2);
  37. btn3 = new Button(this);
  38. btn3.setText("|\n|\n|\n|\n|\n|");
  39. btn3.setId(ID_BTN3);
  40. RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  41. lp3.addRule(RelativeLayout.POSITION_BELOW, ID_BTN1);
  42. lp3.addRule(RelativeLayout.POSITION_TO_RIGHT, ID_BTN2);
  43. lp3.addRule(RelativeLayout.ALIGN_RIGHT, ID_BTN1);
  44. // btn3 位於 btn1 的下方、btn2 的右方且其右邊和 btn1 的右邊對齊(要擴充)
  45. rl.addView(btn3,lp3);
  46. btn4 = new Button(this);
  47. btn4.setText("--------------------------------------------");
  48. btn4.setId(ID_BTN4);
  49. RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  50. lp4.addRule(RelativeLayout.POSITION_BELOW, ID_BTN2);
  51. lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
  52. // btn4 位於 btn2 的下方,在父 Veiw 中水平居中
  53. rl.addView(btn4,lp4);
  54. setContentView(rl);
  55. }
  56. }

注意:

一個控件與其對應的LayoutParams是綁定在一起的。將控件加入到布局中後,改變其LayoutParams仍對這個控件起作用。

例如代碼:

  1. package my.test.userLayout;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.RelativeLayout;
  5. import android.widget.TextView;
  6. public class LyricRoll extends RelativeLayout {
  7. private Context context = null;
  8. public LyricRoll(Context context) {
  9. super(context);
  10. this.context = context;
  11. init();
  12. }
  13. public LyricRoll(Context context, AttributeSet attr) {
  14. super(context, attr);
  15. this.context = context;
  16. init();
  17. }
  18. public LyricRoll(Context context, AttributeSet attr, int i) {
  19. super(context, attr, i);
  20. this.context = context;
  21. init();
  22. }
  23. private void init(){
  24. RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
  25. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  26. TextView txv = new TextView(context);
  27. txv.setId(1);
  28. txv.setText("txv1");
  29. TextView txv2 = new TextView(context);
  30. txv2.setId(2);
  31. txv2.setText("txv2");
  32. lp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
  33. this.addView(txv, lp);
  34. lp.addRule(RelativeLayout.BELOW,1);
  35. RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
  36. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  37. lp2.addRule(RelativeLayout.BELOW,1);
  38. this.addView(txv2,lp2);
  39. lp.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE); //此時更改lp仍舊能夠改變txv的位置。
  40. }
  41. }
Copyright © Linux教程網 All Rights Reserved