1、線性佈局
LinearLayout:控制子控件按照橫向或縱向排列
1. 靈魂屬性:android:orientation=”vertical”;(horizontal)
2. gravity屬性:控制佈局內所有子控件在父容器中的相對位置
-center 水平垂直居中(在中央)
-center_horizontal 水平居中
-center_vertical 垂直居中
-top 頂部
-bottom 底部
-left 靠左
-right 靠右
2、相對佈局
RelativeLayout:子控件靈活的出現在某特殊的位置上;方便描述控件與控件的位置
1. 根據父容器定位
2. 根據兄弟控件定位
3. margin(偏移):設置組件與父容器之間的邊距
4. padding(填充):控件內部元素間的邊距(比如TextView內部字體的位置)
3、幀佈局
FrameLayout:子控件之間是層疊覆蓋的關係;多應用在滑動頁和碎片出現的場合
1. android:foreground: 設置次幀佈局容器的前景圖像
2. android:foregroundGravity: 設置前景圖像顯示的位置
4、表格佈局
TableLayout:確定表格的行數;設置某一行中某列元素 隱藏 拉伸 或 收縮
1. android:collapseColumns:設置需要被隱藏的列的序號
2. android:shrinkColumns:設置允許被收縮的列的列序號
3. android:stretchColumns:設置運行被拉伸的列的列序號
5、網格佈局
GridLayout
1. 設置排列方式
2. 設置幾行幾列
android:rowCount;網格佈局中的行數
android:columnCount; 網格佈局中的列數
3. 設置組件所在的行列
4. 設置組件橫跨幾行幾列
關鍵點一、android:gravity vs android:layout_gravity
- android:gravity設置佈局內部所以的子View
- android:layout_gravity設置View本身
- 在線性佈局中android:layout_gravity屬性的設置與父佈局的
android:orientation有關 - 當 android:orientation=”vertical” 時, 只有水平方向的設置才起作用,垂直方向的設置不起作用。 即:left,right,center_horizontal 是生效的。 當 android:orientation=”horizontal” 時, 只有垂直方向的設置才起作用,水平方向的設置不起作用。 即:top,bottom,center_vertical 是生效的
關鍵點二、layout_weight權重屬性
- 在線性佈局中:若佈局靈魂屬性爲橫向,在子控件中將(寬)
設置爲0dp(android:layout_width=”0dp”),相對最外層佈局則會按照權重屬性分配剩餘空間。縱向排列同理。 - 根據orientation屬性,比如是橫向的,在設置View寬度屬性爲包裹內容,即android:layout_width=”wrap_content” ,則相對最外層佈局則會按照權重屬性分配剩餘空間,借鑑代碼,加強理解。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="three"
android:background="#FF00FF"
/>
</LinearLayout>
3.如果設置
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” 屬性,則需要重新計算,計算依據爲權重每次分的是父容器的剩餘空間(不論是縱向還是橫向)
關鍵點三、爲LinearLayout設置分割線
- 直接添加一條線的View,代碼如下
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />
2.LinearLayout的一個divider屬性