Android中文API(99)—— RelativeLayout
public class?RelativeLayout extends?ViewGroup? ??
java.lang.Object
android.view.View
android.view.ViewGroup
???????? android.widget.RelativeLayout
直接子類
DialerFilter, TwoLineListItem
二、概述
RelativeLayout顧名思義,相對布局,在這個容器內部的子元素們可以使用彼此之間的相對位置或者和容器間的相對位置來進行定位。
注意,不能在RelativeLayout容器本身和他的子元素之間產生循環依賴,比如說,不能將RelativeLayout的高設置成為WRAP_CONTENT的時候將子元素的高設置成為ALIGN_PARENT_BOTTOM。
(譯者注:這點額外要注意,當然這也很好理解,如果容器是不定高的,那么子元素當然無法對齊容器的底邊^_^)。
可以參考官方SDK中的?RelativeLayout教學貼Relative Layout tutorial三、常量
| 數據類型 | 常量名稱 | 描述 |
| 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)內水平居中 |
| int | CENTER_IN_PARENT | 定義讓元素位于容器(RelativeLayout)的中心 |
| int | CENTER_VERTICAL | 定義讓元素在容器(RelativeLayout)內垂直居中 |
| int | LEFT_OF | 定義將元素的右邊對齊另一個元素的左邊 |
| int | RIGHT_OF | 定義將元素的左邊對齊另一個元素的右邊 |
| int | TRUE | ? |
四、內部類
Class ??????RelativeLayout.LayoutParams????
和RelativeLayout相關聯的布局信息.
五、XML屬性
| 屬性名稱 | 描述 |
| android:gravity | 設置容器中的內容該如何定位。對象本身的x和y軸。 |
| android:ignoreGravity | 設置容器中的哪個子元素會不受Gravity的影響. (譯者注:接收參數:子元素的Id,如果設置成為0,則全部子元素都會受到影響) |
六、公共方法
public boolean?dispatchPopulateAccessibilityEvent?(AccessibilityEvent event)
???????????分發?AccessibilityEvent?事件到該?View?的子視圖中。
參數
????????????????? event ??????事件本身.
?????????????????返回值
???????????????????????如果事件分發完成,返回真。
public RelativeLayout.LayoutParams?generateLayoutParams?(AttributeSet attrs)
返回一個新的已設置屬性集合的布局參數。
參數
????????????????? attrs ???????構建layout布局參數的屬性集合.
?????????????????返回值
???????????????????????一個RelativeLayout.LayoutParams的實例或者他的一個子類
public int?getBaseline?()
???????????返回窗口空間的文本基準線到其頂邊界的偏移量。如果這個部件不支持基準線對齊,這個方法返回-1。
??????????? 返回值
????????????????? 基準線的偏移量,如果不支持基準線對齊則返回-1
public void?requestLayout?()
???????????當容器中的某個視圖發生改變,會影響整個布局時,就調用這個方法,他會改變整個視圖樹的布局。
public void?setGravity?(int gravity)
???????????描述子視圖該如何擺放,默認值是Gravity.LEFT | Gravity.TOP(譯者注:說明RelativeLayout的元素如果不設置位置,會從左上角開始堆疊)。
參數
???????????????? gravity ???位置值,可以參考Gravity
?????????????????相關的XML屬性
????????????????? android:gravity
?????????????????參見
????????????????? setHorizontalGravity(int)
?setVerticalGravity(int)
public void?setHorizontalGravity?(int horizontalGravity)
public void?setVerticalGravity?(int verticalGravity))
public void?setIgnoreGravity?(int viewId)
???????????定義容器中的那個VIew會忽略容器設置的Gravity,如果容器設置的Gravity是Gravity.LEFT | Gravity.TOP時,定義這個屬性沒有任何效果。
參數
???????????????? viewId??????將要忽略容器Gravity的視圖的id,設置為0則不會有任何View忽略Gravity.
?????????????????相關的XML屬性
????????????????? android:ignoreGravity
?????????????????參見
? ? ? ? ? ? ? ? ? ? ??setGravity(int)
七、受保護方法
protected boolean?checkLayoutParams?(ViewGroup.LayoutParams p)
??????????(譯者注:檢測是不是AbsoluteLayout.LayoutParams的實例)? ? ?
protected ViewGroup.LayoutParams ?generateDefaultLayoutParams?()
???????????返回一組寬度為WRAP_CONTENT,高度為WRAP_CONTENT,坐標是(0,0)的布局參數。
?????????????????返回值
???????????????????????一組默認的布局參數或null值。
protected ViewGroup.LayoutParams?generateDefaultLayoutParams?( ViewGroup .LayoutParams p)
???????????返回一組合法的受支持的布局參數。當一個ViewGroup傳遞一個布局參數沒有通過checkLayoutParams(android.view.ViewGroup.LayoutParams)檢測的視圖時,此方法被調用。此方法會返回一組新的適合當前ViewGroup的布局參數,可能從指定的一組布局參數中復制適當的屬性。
參數
????????????????? p ?????被轉換成一組適合當前?ViewGroup的布局參數.
?????????????????返回值
???????????????????????一個ViewGroup.LayoutParams的實例或者其中的一個子節點
protected void?onLayout?(boolean changed, int l, int t, int r, int b)
???????????在此視圖view給他的每一個子元素分配大小和位置時調用。?派生類可以重寫此方法并且重新安排他們子類的布局。
參數
???????????????? changed??這是當前視圖view的一個新的大小或位置
? l????????相對于父節點的左邊位置
???? t????????相對于父節點的頂點位置
???? r????????相對于父節點的右邊位置
???? b???????相對于父節點的底部位置
public void?onMeasure?(int widthMeasureSpec, int heightMeasureSpec)
???????????測量視圖以確定其內容寬度和高度。此方法被measure(int, int)調用。需要被子類重寫以提供對其內容準確高效的測量。
約定:當重寫此方法時,你必須調用setMeasuredDimension(int, int)來保存當前視圖view的寬度和高度。不成功調用此方法將會導致一個IllegalStateException異常,是由measure(int, int)拋出。所以調用父類的onMeasure(int, int)方法是必須的。
父類的實現是以背景大小為默認大小,除非MeasureSpec(測量細則)允許更大的背景。子類可以重寫onMeasure(int,int)以對其內容提供更佳的尺寸。
如果此方法被重寫,那么子類的責任是確認測量高度和測量寬度要大于視圖view的最小寬度和最小高度(getSuggestedMinimumHeight() and getSuggestedMinimumWidth()),使用這兩個方法可以取得最小寬度和最小高度。
參數
????????????????? widthMeasureSpec ?????????強加于父節點的橫向空間要求。要求是使用View.MeasureSpec進行編碼。
????????????????? heightMeasureSpec?????????強加于父節點的縱向空間要求。要求是使用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>
本文轉自博客園農民伯伯的博客,原文鏈接:Android中文API(99)—— RelativeLayout,如需轉載請自行聯系原博主。
?
總結
以上是生活随笔為你收集整理的Android中文API(99)—— RelativeLayout的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何禁止使用bottomsheetdia
- 下一篇: android sina oauth2.