1. 程式人生 > >Android線性佈局和相對佈局的詳解和區別

Android線性佈局和相對佈局的詳解和區別

一、線性佈局(LinearLayout)

線性佈局:線性佈局是我們在開發中最常見的佈局方式之一,線性佈局可以分為水平線性佈局和垂直線性佈局這兩種佈局方式

線性佈局的屬性(決定佈局中元素的位置和佈局):

android:layout_gravity ( 是本元素相對於父元素的對齊方式 )

android:gravity="bottom|right"(是本元素所有子元素的對齊方式,設定在父元素上,多個值用|隔開)

android:layout_gravity (子元素在父元素的對齊方式,設定在子元素上)

當 android:orientation="vertical"  時, 只有水平方向的設定才起作用,垂直方向的設定不起作用。即:left(左),right(右),center_horizontal(水平居中) 是生效的。
當 android:orientation="horizontal" 時, 只有垂直方向的設定才起作用,水平方向的設定不起作用。即:top(上),bottom(下),center_vertical (垂直居中)是生效的。

android:padding="10dp" (是本元素所有子元素的與父元素邊緣的距離,設定在父元素上) 

android:layout_marginLeft="10dp"(子元素與父元素邊緣的距離,設定在子元素上)

android:orientation (線性佈局以列或行來顯示內部子元素)

android:layout_weight ="1"  分配分配權重值

這就是線性佈局做出來的效果啦,下邊來說一下權重這個東西。

權重:

android:layout_weight="1"通過設定控制元件的layout_weight屬性以控制各個控制元件在佈局中的相對大小

,線性佈局會根據該控制元件layout_weight值與其所處佈局中所有控制元件layout_weight值之和的比值為該控制元件分配佔用的區域。

在水平佈局的LinearLayout中有4個TxtView,這4個TextView的layout_weight屬性值都為1,那麼這4個TextView的大小將拉伸到總大小的四分之一。如果layout_weight指為0,控制元件會按原大小顯示,不會被拉伸;對於其餘layout_weight屬性值大於0的控制元件,系統將會減去layout_weight屬性值為0的控制元件的寬度或者高度,再用剩餘的寬度或高度按相應的比例來分配每一個控制元件顯示的寬度或高度。

權重最基本的用法就是  對線性佈局指定方向(水平或垂直)上剩餘空間分配的一個規則,先把規定的大小佔完,再來按比例分配剩餘空間

特殊情況:

首先計算數值,所有控制元件加起來後可能超過螢幕大小了,這個時候剩餘值就應該是負的,此時按權重分配,權重大的分得值比較大,但是負的,這個時候加上原來的值,反而變小

 權重有一個很有用的特點,在一些特殊應用場景,比如有兩個控制元件,一個設定了權重,一個不設定權重,那麼這個設定權重的控制元件會後載入渲染。

 

以上就是線性佈局的介紹和屬性啦,接下來說一下相對佈局。

二、相對佈局(RelativeLayout

 

相對佈局:相對佈局也是常用的佈局之一,它可以設定某一個控制元件相對於其他控制元件的位置,這些位置可以包括上下左右等,因此相較於其他的佈局方式而言具有很大的靈活性。

 

相對佈局常用的屬性:

第一類:屬性值為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 離某元素上邊緣的距離

android:gravity  
android:gravity屬性是對該view 內容的限定.比如一個button 上面的text.  你可以設定該text 在view的靠左,靠右等位置.以button為例,android:gravity="right"則button上面的文字靠右 

android:layout_gravity 
android:layout_gravity是用來設定該view相對與起父view 的位置.比如一個button 在linearlayout裡,你想把該button放在靠左、靠右等位置就可以通過該屬性設定.以button為例,android:layout_gravity="right"則button靠右 

android:layout_alignParentRight 
使當前控制元件的右端和父控制元件的右端對齊。這裡屬性值只能為true或false,預設false。 

android:scaleType: 
android:scaleType是控制圖片如何resized/moved來匹對ImageView的size。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 用矩陣來繪製,動態縮小放大圖片來顯示。 

 注意:圖片資料夾裡面的圖片命名是不能大寫的。

說完這兩個佈局,咱們再來看看這兩個佈局的區別。

線性佈局(LinearLayout):在該標籤下的所有子元素會根據orientation屬性的值來決定是按行或者是按列來逐個顯示。程式碼示例如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"   
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" >  
    
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/app_name" />  
   
    <Button  
        android:id="@+id/button2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/hello_world" />  
   
    <Button  
        android:id="@+id/button3"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/test" />  
   
</LinearLayout>

而相對佈局,則是根據控制元件的相對位置而言,比如居於按鈕的左側或者右側,示例如下:

 <RelativeLayout   
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">  
        <Button  
            android:id="@+id/button2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_toRightOf="@id/button1"  
            android:layout_alignTop="@id/button1"  
            android:text="@string/hello_world" />  
   
        <Button  
            android:id="@+id/button1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentTop="true"  
            android:text="@string/app_name" />  
   
    </RelativeLayout>

所以說,線性佈局比較適合所有控制元件都是整齊排列的頁面,相對佈局比較隨意一點,可以按照自己的想法來放置控制元件的位置。但是相對佈局的寫起來比較麻煩一點,需要自己考慮好所有控制元件的的佈局。