Android布局
LinearLayout(線性布局)
RelativeLayout(相對布局)
TableLayout(表格布局)
FrameLayout(幀布局)
AbsoluteLayout(絕對布局)
GridLayout(網格布局)
LinerLayout

weight(權重)屬性詳解
實作代碼
<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">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#ADFF2F"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#DA70D6"
android:layout_weight="2"/>
</LinearLayout>
實作效果

按比例劃分
水平方向:將涉及到的View的android:width屬性設定為0dp,然后設定為android weight屬性設定比例即可;類推,豎直方向,只需設android:height為0dp,然后設weight屬性即可!
RelativeLayout
組件屬性

父容器定位屬性示意圖

兄弟組件定位
所謂的兄弟組件就是處于同一層次容器的組件
圖中的組件1,2就是兄弟組件了,而組件3與組件1或組件2并不是兄弟組件,所以組件3不能通過 組件1或2來進行定位,比如layout_toleftof = "組件1"這樣是會報錯的!切記! 關于這個兄弟組件定位的最經典例子就是"梅花布局"了

//實作代碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 這個是在容器中央的 -->
<ImageView
android:id="@+id/img1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:src="@drawable/pic1"/>
<!-- 在中間圖片的左邊 -->
<ImageView
android:id="@+id/img2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_toLeftOf="@id/img1"
android:layout_centerVertical="true"
android:src="@drawable/pic2"/>
<!-- 在中間圖片的右邊 -->
<ImageView
android:id="@+id/img3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_toRightOf="@id/img1"
android:layout_centerVertical="true"
android:src="@drawable/pic3"/>
<!-- 在中間圖片的上面-->
<ImageView
android:id="@+id/img4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_above="@id/img1"
android:layout_centerHorizontal="true"
android:src="@drawable/pic4"/>
<!-- 在中間圖片的下面 -->
<ImageView
android:id="@+id/img5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@id/img1"
android:layout_centerHorizontal="true"
android:src="@drawable/pic5"/>
</RelativeLayout>
TableLayout
屬性圖

如何確定行數與列數
①如果我們直接往TableLayout中添加組件的話,那么這個組件將占滿一行!!!
②如果我們想一行上有多個組件的話,就要添加一個TableRow的容器,把組件都丟到里面!
③tablerow中的組件個數就決定了該行有多少列,而列的寬度由該列中最寬的單元格決定
④tablerow的layout_width屬性,默認是fill_parent的,我們自己設定成其他的值也不會生效!!! 但是layout_height默認是wrapten——content的,我們卻可以自己設定大小!
⑤整個表格布局的寬度取決于父容器的寬度(占滿父容器本身)
⑥有多少行就要自己數啦,一個tablerow一行,一個單獨的組件也一行!多少列則是看tableRow中 的組件個數,組件最多的就是TableLayout的列數
三個常用屬性
android:collapseColumns:設定需要被隱藏的列的序號
android:shrinkColumns:設定允許被收縮的列的列序號
android:stretchColumns:設定運行被拉伸的列的列序號
以上這三個屬性的列號都是
從0開始算的,比如shrinkColunmns = “2”,對應的是第三列!
可以設定多個,用逗號隔開比如"0,2",如果是所有列都生效,則用"*"號即可
除了這三個常用屬性,還有兩個屬性,分別就是跳格子以及合并單元格,這和HTML中的Table類似:
android:layout_column="2":表示的就是跳過第二個,直接顯示到第三個格子處,從1開始算的!
android:layout_span="4":表示合并4個單元格,也就說這個組件占4個單元格
FrameLayout
常用屬性
前景影像:永遠處于幀布局最上面,直接面對用戶的影像,就是不會被覆寫的圖片,
- android:foreground:*設定改幀布局容器的前景影像
- android:foregroundGravity:設定前景影像顯示的位置
GridLayout
特點
- 可以自己設定布局中組件的排列方式
- 可以自定義網格布局有多少行,多少列
- 可以直接設定組件位于某行某列
- 可以設定組件橫跨幾行或者幾列
屬性

AbsoluteLayout
幾乎不做使用,略過
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/262952.html
標籤:其他
