Framelayout
-
框架布局
框架布局是用來占據(jù)屏幕一塊區(qū)域來顯示一個(gè)單一的item.通常,框架布局應(yīng)該擁有單一的child view,因?yàn)楹茈y去管理多個(gè)child view,當(dāng)你去適配views防止這些child view 相互覆蓋的時(shí)候.當(dāng)然,你也可以添加多個(gè)children到框架布局中,通過控制他們的位置即框架布局分配gravity到每一個(gè)child view.
通過layout_gravity設(shè)定位置.通過layout_margin來設(shè)置間距
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="居中" android:layout_gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="左邊" android:layout_gravity="left"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="距離頂部20dp,右邊20dp" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:layout_marginStart="20dp"/> </FrameLayout>
AbsoluteLayout
- 絕對(duì)布局(google不建議使用絕對(duì)布局)
能夠使子View在某一個(gè)固定的位置.絕對(duì)布局缺乏靈活性,而且很難去維護(hù)相比于其他沒有絕對(duì)位置的布局.
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="100dp"
android:text="文本"
/>
</AbsoluteLayout>
Relativelayout
- 相對(duì)布局
布局位置可以根據(jù)周圍相互關(guān)聯(lián)的布局或者父布局來設(shè)置.
官方提示:
Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.
不能用于一個(gè)循壞依賴的布局在相對(duì)布局尺寸和子布局之間.舉個(gè)例子,當(dāng)你Relativelayout的高度是WRAP_CONTENT,那么Relativelayout的尺寸是相對(duì)的,這個(gè)時(shí)候你設(shè)置子View 為ALIGN_PARENT_BOTTOM,也是依賴于父布局的底邊的位置,這樣是不行的.
在android系統(tǒng)版本在17級(jí)以下(包含17的時(shí)候)。使用measure會(huì)出現(xiàn)NULL異常情況,這個(gè)是一個(gè)BUG。原因是在RelativeLayout的控件使用在含有scrolling的時(shí)候,該含有scrolling的控件中計(jì)算空間大小的時(shí)候,沒有使用MeasureSpec mode UNSPECIFIED的布局方式在RelativeLayout。自定義的控件則會(huì)盡可能的使用AT_MOST 來替換對(duì)齊方式。
如果你想解決這個(gè)問題有2個(gè)方法:
1.講SDK的目標(biāo)版本升級(jí)
2.將需要使用RelativeLayout的上層包一個(gè)LinearLayout即可.
- 甲在乙的____邊
| 屬性 | 解釋 |
|---|---|
| Above | 定義將元素的底邊對(duì)齊另一個(gè)元素的頂邊 |
| BELOW | 定義將元素的頂邊對(duì)齊另一個(gè)元素的底邊 |
- 甲和乙對(duì)齊____邊
| 屬性 | 解釋 |
|---|---|
| align_baseline | 定義將元素的基線對(duì)齊另一個(gè)元素的基線 |
| align_bottom | 定義將元素的底邊對(duì)齊另一個(gè)元素的底邊 |
| align_left | 定義將元素的左邊對(duì)齊另一個(gè)元素的左邊 |
| align_right | 定義將元素的右邊對(duì)齊另一個(gè)元素的右邊 |
| align_top | 定義將元素的頂邊對(duì)齊另一個(gè)元素的頂邊 |
- 甲對(duì)齊父容器__邊
| 屬性 | 解釋 |
|---|---|
| align_parent_bottom | 定義將元素的底邊對(duì)齊父容器的底邊 |
| align_parent_left | 定義將元素的左邊對(duì)齊父容器的左邊 |
| align_parent_right | 定義將元素的右邊對(duì)齊父容器的右邊 |
| align_parent_top | 定義將元素的頂邊對(duì)齊父容器的頂邊 |
- 甲在容器__邊
| 屬性 | 解釋 |
|---|---|
| CENTER_HORIZONTAL | 定義元素在RelativeLayout中水平居中 |
| CENTER_IN_PARENT | 定義元素在RelativeLayout的中心 |
| CENTER_VERTICAL | 定義元素在RelativeLayout內(nèi)垂直居中 |
- 甲的邊對(duì)齊已的邊
| 屬性 | 解釋 |
|---|---|
| LEFT_OF | 甲元素的右邊對(duì)齊乙元素的左邊 |
| RIGHT_OF | 甲元素的左邊對(duì)齊乙元素的右邊 |
LinearLayout
- 線性布局
A Layout that arranges its children in a single column or a single row. The direction of the row can be set by calling setOrientation(). You can also specify gravity, which specifies the alignment of all the child elements by calling setGravity() or specify that specific children grow to fill up any remaining space in the layout by setting the weight member of LinearLayout.LayoutParams. The default orientation is horizontal
一個(gè)可以將子元素分布到一列或者一行的布局.可以通過setOrientation來設(shè)置水平或者垂直方向.你也可以通過setGravity來確定重力方向.通過weight屬性來設(shè)置元素在布局中占據(jù)的比例.
-
線性布局中,Orientation和Gravity存在兩條規(guī)則:
當(dāng) android:orientation="vertical" 時(shí), 只有水平方向的設(shè)置才起作用,垂直方向的設(shè)置不起作用。即:left,right,center_horizontal 是生效的。
當(dāng) android:orientation="horizontal" 時(shí), 只有垂直方向的設(shè)置才起作用,水平方向的設(shè)置不起作用。即:top,bottom,center_vertical 是生效的。
例子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本1"
android:layout_gravity="end"
/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本2"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
2:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本1"
android:layout_gravity="top"
/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本2"
android:layout_gravity="bottom"
/>
</LinearLayout>
- layout_gravity和gravity的區(qū)別
android:gravity是相對(duì)于自己的位置,如button中的text相對(duì)自己的位置.
android:layout_gravity是相對(duì)于布局的位置.如屏幕居中.
android:layout_gravity只在LinearyLayout和FrameLayout中有效.