Android Layout 之 RelativeLayout RelativeLayout.LayoutParams


使用 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.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.*;  
  6. import android.view.*;  
  7.   
  8. public class RLTest extends Activity {  
  9.   
  10. private RelativeLayout rl;  
  11.   
  12. private Button btn1;  
  13. private Button btn2;  
  14. private Button btn3;  
  15. private Button btn4;  
  16.   
  17. private static final int ID_BTN1 = 1;  
  18. private static final int ID_BTN2 = 2;  
  19. private static final int ID_BTN3 = 3;  
  20. private static final int ID_BTN4 = 4;  
  21.   
  22. /** Called when the activity is first created. */  
  23. @Override  
  24. public void onCreate(Bundle icicle) {  
  25. super.onCreate(icicle);  
  26.   
  27. rl = new RelativeLayout(this);  
  28.   
  29. btn1 = new Button(this);  
  30. btn1.setText("----------------------");  
  31. btn1.setId(ID_BTN1);  
  32.   
  33. RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  34. lp1.addRule(RelativeLayout.ALIGN_WITH_PARENT_TOP);  
  35. lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);   
  36. // btn1 位于父 View 的顶部,在父 View 中水平居中   
  37. rl.addView(btn1, lp1 );  
  38.   
  39. btn2 = new Button(this);  
  40. btn2.setText("|\n|\n|\n|\n|\n|");  
  41. btn2.setId(ID_BTN2);  
  42.   
  43. RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  44. lp2.addRule(RelativeLayout.POSITION_BELOW, ID_BTN1);  
  45. lp2.addRule(RelativeLayout.ALIGN_LEFT, ID_BTN1);  
  46. // btn2 位于 btn1 的下方、其左边和 btn1 的左边对齐   
  47. rl.addView(btn2, lp2);  
  48.   
  49. btn3 = new Button(this);  
  50. btn3.setText("|\n|\n|\n|\n|\n|");  
  51. btn3.setId(ID_BTN3);  
  52.   
  53. RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  54. lp3.addRule(RelativeLayout.POSITION_BELOW, ID_BTN1);  
  55. lp3.addRule(RelativeLayout.POSITION_TO_RIGHT, ID_BTN2);  
  56. lp3.addRule(RelativeLayout.ALIGN_RIGHT, ID_BTN1);  
  57. // btn3 位于 btn1 的下方、btn2 的右方且其右边和 btn1 的右边对齐(要扩充)   
  58. rl.addView(btn3,lp3);  
  59.   
  60. btn4 = new Button(this);  
  61. btn4.setText("--------------------------------------------");  
  62. btn4.setId(ID_BTN4);  
  63.   
  64. RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  65. lp4.addRule(RelativeLayout.POSITION_BELOW, ID_BTN2);  
  66. lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);  
  67. // btn4 位于 btn2 的下方,在父 Veiw 中水平居中   
  68. rl.addView(btn4,lp4);  
  69.   
  70.   
  71. setContentView(rl);  
  72. }  
  73. }  

注意:

一个控件与其对应的LayoutParams是绑定在一起的。将控件加入到布局中后,改变其LayoutParams仍对这个控件起作用。

例如代码:

  1. package my.test.userLayout;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.RelativeLayout;  
  6. import android.widget.TextView;  
  7.   
  8. public class LyricRoll extends RelativeLayout {  
  9.   
  10.     private Context context = null;  
  11.     public LyricRoll(Context context) {  
  12.         super(context);  
  13.         this.context = context;  
  14.         init();  
  15.     }  
  16.     public LyricRoll(Context context, AttributeSet attr) {  
  17.         super(context, attr);  
  18.         this.context = context;  
  19.         init();  
  20.     }  
  21.   
  22.     public LyricRoll(Context context, AttributeSet attr, int i) {  
  23.         super(context, attr, i);  
  24.         this.context = context;  
  25.         init();  
  26.     }  
  27.     private void init(){  
  28.         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(  
  29.                                                     LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);   
  30.         TextView txv = new TextView(context);  
  31.         txv.setId(1);  
  32.         txv.setText("txv1");  
  33.         TextView txv2 = new TextView(context);  
  34.         txv2.setId(2);  
  35.         txv2.setText("txv2");  
  36.         lp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);  
  37.         this.addView(txv, lp);  
  38.         lp.addRule(RelativeLayout.BELOW,1);  
  39.         RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(  
  40.                 LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);   
  41.         lp2.addRule(RelativeLayout.BELOW,1);  
  42.           
  43.         this.addView(txv2,lp2);  
  44.         lp.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);     //此时更改lp仍旧能够改变txv的位置。   
  45.           
  46.     }  
  47.   
  48.       
  49.   
  50. }  

相关内容