RelativeLayout布局是Android界面布局中應用最廣也最強大的一種布局,其不僅十分靈活,可以解決開發中各種界面布局需求,同時也很友善了解決了多螢幕尺寸的适配問題。在iOS開發中,Autolayout技術總是被贊不絕口,RelativeLayout布局就是Andriod系統中的Autolayout,其又被稱為相對布局。
所謂相對布局,是指其坐标的确定并不是開發者寫死的,而是有系統自動計算出來的,那麼系統如何計算每個視圖控件的位置呢?開發者需要為其添加一些規則進行限制,這些規則大緻包括2類:
第1類 與父視圖之間位置關系的規則:
此類規則包括在父視圖中的居中、左對齊、右對齊、上對齊、下對齊等。
第2類 平級視圖之間相對位置關系的規則:
此類規則包括同級視圖間對其關系,相對位置關系,例如A在B左側20像素位置,B與C上邊緣對齊等。
使用RelativeLayout進行布局示例代碼如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout relativeLayout = new RelativeLayout(this);
Button button1 = new Button(this);
button1.setText("按鈕一");
button1.setId(R.id.button1);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(200,200);
//添加限制 使其靠近父視圖右上角
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button1.setLayoutParams(layoutParams1);
Button button2 = new Button(this);
button2.setText("按鈕二");
button2.setId(R.id.button2);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(200,200);
//添加限制 讓其右側靠近按鈕一左側 上側靠近按鈕一下側
layoutParams2.addRule(RelativeLayout.BELOW,R.id.button1);
layoutParams2.addRule(RelativeLayout.LEFT_OF,R.id.button1);
button2.setLayoutParams(layoutParams2);
Button button3 = new Button(this);
button3.setText("按鈕三");
button3.setId(R.id.button3);
RelativeLayout.LayoutParams layoutParams3 = new RelativeLayout.LayoutParams(200,200);
//添加限制 讓其固定距離按鈕二 下方100px 左側邊緣對其
layoutParams3.addRule(RelativeLayout.ALIGN_LEFT,R.id.button2);
layoutParams3.addRule(RelativeLayout.BELOW,R.id.button2);
layoutParams3.topMargin = 100;
button3.setLayoutParams(layoutParams3);
relativeLayout.addView(button1);
relativeLayout.addView(button2);
relativeLayout.addView(button3);
setContentView(relativeLayout);
}
小提示:使用代碼建立的視圖,可以通過xml檔案配置id,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="button1" type="id"></item>
<item name="button2" type="id"></item>
<item name="button3" type="id"></item>
</resources>
效果如下圖:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5yMyIGNzgjZilzMyQzYiFWYxgjN0EjZhBjMmNDZzYjYx8CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
RelativeLayout布局中視圖位置的配置主要使用其内部類LayoutParams,這個内部類LayoutParams是繼承自MarginLayoutParams。其中常用方法和屬性列舉如下:
//設定左邊距
public int leftMargin;
//設定上邊距
public int topMargin;
//設定右邊距
public int rightMargin;
//設定下邊距
public int bottomMargin;
//添加一個規則 這個方法添加的規則不需要參照視圖 例如靠近父視圖邊緣
public void addRule(int verb)
//添加一個規則 這個方法添加的規則需要一個參照視圖 例如某兩個平級視圖間的位置關系 anchor參數為視圖id
public void addRule(int verb, int anchor)
//移除一個布局規則
public void removeRule(int verb)
用于進行布局規則配置的參數如下:
/*=======需要使用addRule(int verb, int anchor)方法添加的限制規則==========*/
//将目前視圖限制到某個視圖左邊
public static final int LEFT_OF
//将目前視圖限制到某個視圖右邊
public static final int RIGHT_OF
//将目前視圖限制到某個視圖上邊
public static final int ABOVE
//将目前視圖限制到某個視圖下邊
public static final int BELOW
//将目前視圖限制與某個視圖基線對齊
public static final int ALIGN_BASELINE
//将目前視圖限制與某個視圖左側對齊
public static final int ALIGN_LEFT
//将目前視圖限制與某個視圖上側對齊
public static final int ALIGN_TOP
//将目前視圖限制與某個視圖右側對齊
public static final int ALIGN_RIGHT
//将目前視圖限制與某個視圖下側對齊
public static final int ALIGN_BOTTOM
//将目前視圖限制與某個視圖起始對齊
public static final int START_OF
//當目前視圖限制與某個視圖末尾對齊
public static final int END_OF
/*========需要使用addRule(int verb)方法添加的限制規則====================*/
//限制目前視圖與父視圖左側對齊
public static final int ALIGN_PARENT_LEFT
//限制目前視圖與父視圖上側對齊
public static final int ALIGN_PARENT_TOP
public static final int ALIGN_PARENT_RIGHT
//限制目前視圖與父視圖下側對齊
public static final int ALIGN_PARENT_BOTTOM
//限制目前視圖與父視圖居中對齊
public static final int CENTER_IN_PARENT
//限制目前視圖與父視圖水準居中
public static final int CENTER_HORIZONTAL
//限制目前視圖與父視圖垂直居中
public static final int CENTER_VERTICAL
//限制目前視圖與父視圖起始對齊
public static final int ALIGN_PARENT_START
//限制目前視圖與父視圖末尾對齊
public static final int ALIGN_PARENT_END