?
? ????? PHP ??? ???? ??? ?? ??
RelativeLayout
版本:Android 2.3 r1
?
結(jié)構(gòu)
繼承關(guān)系
public class RelativeLayout extends ViewGroup
????????
java.lang.Object
android.view.View
android.view.ViewGroup
???????? android.widget.RelativeLayout
?
子類及間接子類
直接子類
???????? ???????? DialerFilter, TwoLineListItem
?
類概述
RelativeLayout顧名思義,相對布局,在這個容器內(nèi)部的子元素們可以使用彼此之間的相對位置或者和容器間的相對位置來進行定位。
注意,不能在RelativeLayout容器本身和他的子元素之間產(chǎn)生循環(huán)依賴,比如說,不能將RelativeLayout的高設(shè)置成為WRAP_CONTENT
的時候?qū)⒆釉氐母咴O(shè)置成為 ALIGN_PARENT_BOTTOM
。
(譯者注:這點額外要注意,當然這也很好理解,如果容器是不定高的,那么子元素當然無法對齊容器的底邊^_^)。
可以參考官方SDK中的 RelativeLayout教學貼Relative Layout tutorial
?
常量
數(shù)據(jù)類型 |
常量名稱 |
描述 |
int |
ABOVE |
定義將元素的底邊對齊另一個元素的頂邊 |
int |
ALIGN_BASELINE |
定義將元素基線的對齊另一個元素的基線 |
int |
ALIGN_BOTTOM |
定義將元素底邊的對齊另一個元素的底邊 |
int |
ALIGN_LEFT |
定義將元素的左邊對齊另一個元素的左邊 |
int |
ALIGN_PARENT_BOTTOM |
定義將元素的底邊對齊其父容器(RelativeLayout)的底邊 |
int |
ALIGN_PARENT_LEFT |
定義將元素的左邊對齊其父容器(RelativeLayout)的左邊 |
int |
ALIGN_PARENT_RIGHT |
定義將元素的右邊對齊其父容器(RelativeLayout)的右邊 |
int |
ALIGN_PARENT_TOP |
定義將元素的頂邊對齊其父容器(RelativeLayout)的頂邊 |
int |
ALIGN_RIGHT |
定義將元素的右邊對齊另一個元素的右邊 |
int |
ALIGN_TOP |
定義將元素的頂邊對齊另一個元素的頂邊 |
int |
BELOW |
定義將元素的頂邊對齊另一個元素的底邊 |
int |
CENTER_HORIZONTAL |
定義讓元素在容器(RelativeLayout)內(nèi)水平居中 |
int |
CENTER_IN_PARENT |
定義讓元素位于容器(RelativeLayout)的中心 |
int |
CENTER_VERTICAL |
定義讓元素在容器(RelativeLayout)內(nèi)垂直居中 |
int |
LEFT_OF |
定義將元素的右邊對齊另一個元素的左邊 |
int |
RIGHT_OF |
定義將元素的左邊對齊另一個元素的右邊 |
int |
TRUE |
? |
?
內(nèi)部類
Class ????? RelativeLayout.LayoutParams ???
和RelativeLayout相關(guān)聯(lián)的布局信息.
?
XML屬性
屬性名稱 |
描述 |
android:gravity |
設(shè)置容器中的內(nèi)容該如何定位。對象本身的x和y軸。 |
android:ignoreGravity |
設(shè)置容器中的哪個子元素會不受Gravity的影響. (譯者注:接收參數(shù):子元素的Id,如果設(shè)置成為0,則全部子元素都會受到影響) |
?
公共方法
public boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)
???????? 分發(fā) AccessibilityEvent
事件到該 View
的子視圖中。
參數(shù)
??????????????????????????? event ????? 事件本身.
?????????????????? 返回值
??????????????????????????? 如果事件分發(fā)完成,返回真。
?
public RelativeLayout.LayoutParams generateLayoutParams (AttributeSet attrs)
返回一個新的已設(shè)置屬性集合的布局參數(shù)。
參數(shù)
??????????????????????????? attrs ?????? 構(gòu)建layout布局參數(shù)的屬性集合.
?????????????????? 返回值
??????????????????????????? 一個RelativeLayout.LayoutParams的實例或者他的一個子類
?
public int getBaseline ()
???????? 返回窗口空間的文本基準線到其頂邊界的偏移量。如果這個部件不支持基準線對齊,這個方法返回-1。
?????????????????? 返回值
??????????????????????????? 基準線的偏移量,如果不支持基準線對齊則返回-1
?
public void requestLayout ()
???????? 當容器中的某個視圖發(fā)生改變,會影響整個布局時,就調(diào)用這個方法,他會改變整個視圖樹的布局。
?
public void setGravity (int gravity)
???????? 描述子視圖該如何擺放,默認值是Gravity.LEFT | Gravity.TOP(譯者注:說明RelativeLayout的元素如果不設(shè)置位置,會從左上角開始堆疊)。
參數(shù)
??????????????????????????? gravity
?? 位置值,可以參考Gravity
?????????????????? 相關(guān)的XML屬性
??????????????????????????? android:gravity
?????????????????? 參見
??????????????????????????? setHorizontalGravity(int)
setVerticalGravity(int)
?
public void setHorizontalGravity (int horizontalGravity)
?
public void setVerticalGravity (int verticalGravity))
?
public void setIgnoreGravity (int viewId)
???????? 定義容器中的那個VIew會忽略容器設(shè)置的Gravity,如果容器設(shè)置的Gravity是Gravity.LEFT | Gravity.TOP時,定義這個屬性沒有任何效果。
參數(shù)
??????????????????????????? viewId????? 將要忽略容器Gravity的視圖的id,設(shè)置為0則不會有任何View忽略Gravity.
?????????????????? 相關(guān)的XML屬性
??????????????????????????? android:ignoreGravity
?????????????????? 參見
??????????????????????????? setGravity(int)
?
受保護方法
protected boolean checkLayoutParams (ViewGroup.LayoutParams p)
???????? (譯者注:檢測是不是AbsoluteLayout.LayoutParams的實例)?????
?
protected ViewGroup.LayoutParams ?generateDefaultLayoutParams ()
???????? 返回一組寬度為WRAP_CONTENT,高度為WRAP_CONTENT,坐標是(0,0)的布局參數(shù)。
?????????????????? 返回值
??????????????????????????? 一組默認的布局參數(shù)或null值。
?
protected ViewGroup.LayoutParams generateDefaultLayoutParams ( ViewGroup .LayoutParams p)
???????? 返回一組合法的受支持的布局參數(shù)。當一個ViewGroup傳遞一個布局參數(shù)沒有通過checkLayoutParams(android.view.ViewGroup.LayoutParams)檢測的視圖時,此方法被調(diào)用。此方法會返回一組新的適合當前ViewGroup的布局參數(shù),可能從指定的一組布局參數(shù)中復(fù)制適當?shù)膶傩浴?/span>
參數(shù)
??????????????????????????? p ???? 被轉(zhuǎn)換成一組適合當前 ViewGroup的布局參數(shù).
?????????????????? 返回值
??????????????????????????? 一個ViewGroup.LayoutParams的實例或者其中的一個子節(jié)點
?
protected void onLayout (boolean changed, int l, int t, int r, int b)
???????? 在此視圖view給他的每一個子元素分配大小和位置時調(diào)用。 派生類可以重寫此方法并且重新安排他們子類的布局。
參數(shù)
??????????????????????????? changed?? 這是當前視圖view的一個新的大小或位置
l??????? 相對于父節(jié)點的左邊位置
t??????? 相對于父節(jié)點的頂點位置
r??????? 相對于父節(jié)點的右邊位置
b?????? 相對于父節(jié)點的底部位置
?
public void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
???????? 測量視圖以確定其內(nèi)容寬度和高度。此方法被measure(int, int)調(diào)用。需要被子類重寫以提供對其內(nèi)容準確高效的測量。
約定:當重寫此方法時,你必須調(diào)用setMeasuredDimension(int, int)來保存當前視圖view的寬度和高度。不成功調(diào)用此方法將會導致一個IllegalStateException異常,是由measure(int, int)拋出。所以調(diào)用父類的onMeasure(int, int)方法是必須的。
父類的實現(xiàn)是以背景大小為默認大小,除非MeasureSpec(測量細則)允許更大的背景。子類可以重寫onMeasure(int,int)以對其內(nèi)容提供更佳的尺寸。
如果此方法被重寫,那么子類的責任是確認測量高度和測量寬度要大于視圖view的最小寬度和最小高度(getSuggestedMinimumHeight() and getSuggestedMinimumWidth()),使用這兩個方法可以取得最小寬度和最小高度。
參數(shù)
??????????????????????????? widthMeasureSpec ???????? 強加于父節(jié)點的橫向空間要求。要求是使用View.MeasureSpec進行編碼。
??????????????????????????? heightMeasureSpec???????? 強加于父節(jié)點的縱向空間要求。要求是使用View.MeasureSpec進行編碼。
?
補充
文章精選
???????? [Android學習指南] 相對布局 RelativeLayout
???????? Android RelativeLayout 屬性
???????? ?RelativeLayout 排版 _ 免寫程式 [blogspot]
示例代碼
<?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="fill_parent">
??? <TextView
??????? android:id="@+id/label"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="Type here:"/>
??? <EditText
??????? android:id="@+id/entry"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:background="@android:drawable/editbox_background"
??????? android:layout_below="@id/label"/>
??? <Button
??????? android:id="@+id/ok"
??????? 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="OK" />
??? <Button
????? ??android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:layout_toLeftOf="@id/ok"
??????? android:layout_alignTop="@id/ok"
??????? android:text="Cancel" />
</RelativeLayout>