一、Android中常用的5大布局方式有以下幾種:
1.線性佈局(LinearLayout):按照垂直或者水平方向佈局的組件;
2.幀佈局(FrameLayout) :組件從屏幕左上方(0,0)佈局組件;
3.相對佈局 (RelativeLayout) :相對其它組件的佈局方式;
4.表格佈局 (TableLayout) :按照行列方式佈局組件;
5.絕對佈局 (AbsoluteLayout):按照絕對座標來佈局組件(不常用,瞭解即可)。
二、詳細介紹:
1.線性佈局(LinearLayout):
按照垂直或者水平的順序依次排列子元素,每一個子元素都位於前一個元素之後。如果是垂直排列,那麼將是一個N行單列的結構,每一行只會有一個元素,而不論這個元素的寬度爲多少;如果是水平排列,那麼將是一個單行N列的結構。如果搭建兩行兩列的結構,通常的方式是先垂直排列兩個元素,每一個元素裏再包含一個LinearLayout進行水平排列。
LinearLayout中的子元素屬性android:layout_weight生效,它用於描述該子元素在剩餘空間中佔有的大小比例。加入一行只有一個文本框,那麼它的默認值就爲0,如果一行中有兩個等長的文本框,那麼他們的android:layout_weight值可以是同爲1。如果一行中有兩個不等長的文本框,那麼他們的android:layout_weight值分別爲1和2,那麼第一個文本框將佔據剩餘空間的三分之二,第二個文本框將佔據剩餘空間中的三分之一。android:layout_weight遵循數值越小,重要度越高的原則。
<!--線性佈局:按垂直或者水平方向佈局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal" //橫向佈局
**android:layout_weight="1"** //該屬性會平分空間,每個人1/4寬。
android:id="@+id/lw1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_weight="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="blue"
android:background="#0000aa"
android:gravity="center_horizontal"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#ffa500"
android:text="orange"
android:gravity="center_horizontal"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#008080"
android:text="teal"
android:gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical" //縱向佈局
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/lw1" //在id爲lw1的控件下佈局,實際上該屬性是相對佈局的。
android:layout_weight="1">
<TextView
android:text="row one"
android:background="#aa0000"
android:textSize="25sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<!-- -->
<TextView
android:text="row two"
android:background="#dda0dd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:text="row three"
android:background="#008080"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:text="row four"
android:background="#ffa500"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
2.幀佈局(FrameLayout):
五大布局中最簡單的一個佈局,從座標(0,0)開發。在這個佈局中,整個界面被當成一塊空白備用區域,所有的子元素都不能被指定放置的位置,它們統統放於這塊區域的左上角,並且後面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和全部遮擋。
<!--<!–幀佈局:從左上角(0,0)開始佈局–>-->
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#00bfff"/>
<TextView
android:layout_width="260dp"
android:layout_height="260dp"
android:background="#ffc0cb"/>
<TextView
android:layout_width="220dp"
android:layout_height="220dp"
android:background="#0000ff"/>
<TextView
android:layout_width="3dp"
android:layout_height="3dp"
android:background="#fa524e"/>
3.相對佈局 (RelativeLayout):
按照各子元素之間的位置關係完成佈局。在此佈局中的子元素裏與位置相關的屬性將生效。
RelativeLayout用到的一些重要的屬性:
<!--相對佈局:按照組件的相對位置來佈局-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tev"
android:text="please type here:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tev"
android:id="@+id/tex"/>
<Button
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:layout_below="@id/tex"
android:text="確定"
/>
<Button
android:layout_marginLeft="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_below="@id/tex"
android:layout_marginRight="30dp"
android:text="取消"/>
4.表格佈局 (TableLayout):
此佈局爲表格佈局,適用於N行N列的佈局格式。一個TableLayout由許多TableRow組成,一個TableRow就代表TableLayout中的一行。
TableRow是LinearLayout的子類,它的android:orientation屬性值恆爲horizontal,並且它的android:layout_width和android:layout_height屬性值恆爲MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是橫向排列,並且寬高一致的。這樣的設計使得每個TableRow裏的子元素都相當於表格中的單元格一樣。在TableRow中,單元格可以爲空,但是不能跨列。
<!--1.表格佈局: 一個viewgroup以表格顯示它的子視圖(view)元素,
即行和列標識一個視圖的位置-->
<TableRow
android:layout_width="wrap_content"
android:layout_height="100dp">
<Button
android:text="button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="button3"
android:layout_width="match_parent" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp">
<Button
android:text="button4"/>
<Button
android:layout_span="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button5"/>
</TableRow>
5.絕對佈局 (AbsoluteLayout):
在此佈局中的子元素的android:layout_x和android:layout_y屬性將生效,用於描述該子元素的座標位置。屏幕左上角爲座標原點(0,0),第一個0代表橫座標,向右移動此值增大,第二個0代表縱座標,向下移動,此值增大。在此佈局中的子元素可以相互重疊。在實際開發中,通常不採用此佈局格式,因爲它的界面代碼過於剛性,以至於有可能不能很好的適配各種終端。故瞭解一下就可以。
三、常用佈局屬性
第一類:屬性值爲true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相對於父元素完全居中
android:layout_alignParentBottom 貼緊父元素的下邊緣
android:layout_alignParentLeft 貼緊父元素的左邊緣
android:layout_alignParentRight 貼緊父元素的右邊緣
android:layout_alignParentTop 貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物
第二類:屬性值必須爲id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左邊
android:layout_toRightOf 在某元素的右邊
android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
第三類:屬性值爲具體的像素值,如30dip,40px
android:layout_marginBottom 離某元素底邊緣的距離
android:layout_marginLeft 離某元素左邊緣的距離
android:layout_marginRight 離某元素右邊緣的距離
android:layout_marginTop 離某元素上邊緣的距離
第四類:其他屬性值
EditText的android:hint 設置EditText爲空時輸入框內的提示信息。
android:gravity 對該view 內容的限定.比如一個button 上面的text. 你可以設置該text 在view的靠左,靠右等位置.以button爲例,android:gravity="right"則button上面的文字靠右
android:layout_gravity 用來設置該view相對與起父view 的位置.比如一個button 在linearlayout裏,你想把該button放在靠左、靠右等位置就可以通過該屬性設置.以button爲例,android:layout_gravity="right"則button靠右
android:scaleType: 控制圖片如何resized/moved來匹對ImageView的size。
第五類:imageview屬性值
ImageView.ScaleType / android:scaleType值的意義區別:
CENTER /center 按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示
CENTER_CROP / centerCrop 按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬)
CENTER_INSIDE / centerInside 將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬
FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到View的寬度,居中顯示
FIT_END / fitEnd 把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置
FIT_START / fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置
FIT_XY / fitXY 把圖片不按比例擴大/縮小到View的大小顯示
MATRIX / matrix 用矩陣來繪製,動態縮小放大圖片來顯示。