Android_Layout (一)

來源:互聯網
上載者:User

標籤:之間   1.0   相對布局   不能   encoding   sid   drawable   ssi   目前時間   

layout (布局)  --->Android 有五大布局,分別是:

  • LinearLayout : 線性布局,子組件按照垂直或者水平方向來布局。
  • RelativeLayout :相對布局,按照控制項之間的相互位置進行參照物的概念排布,存在一個排布,存在一個參照物的概念, 一般來說在相對布局中的控制項都會存在id 的屬性。
  • TableLayout :表格版面配置,繼承於線性布局。(用法類似)
  • AbsoluteLayout :絕對布局,是通過指定控制項的x/y座標來定位的(顯示效果死板,不利於開發,幾乎被棄用)。
  • FrameLayout :幀布局,畫面顯示的效果
一、線性布局(LiearLayout) 

    線性布局在開發中使用最多,具有垂直方向與水平方向的布局方式
  通過設定屬性“android:orientation”控制方向,屬性值垂直(vertical)和水平(horizontal),預設水平方向。

 常用屬性:

  android:layout_gravity 本元素相對於父元素的重力方向

  android:gravity 本元素所有子項目的重力方向

  android:orientation 線性布局以列或行來顯示內部子項目

  android:layout_weight 子項目對未佔用空間水平或垂直分配權重值


  當 android:orientation="vertical" 時, 只有水平方向的設定才起作用,垂直方向的設定不起作用。即:left,right,center_horizontal 是生效的。!!!!

  當 android:orientation="horizontal" 時, 只有垂直方向的設定才起作用,水平方向的設定不起作用。即:top,bottom,center_vertical 是生效的。!!!!

android:layout_gravity 和 android:gravity 的區別

  android:gravity 對元素本身起作用-本身元素顯示在什麼位置

  android:layout_gravity 相對與它的父元素-元素顯示在父元素的什麼位置。

  如:Button控制項

    android:layout_gravity 表示button在介面上的位置

    android:gravity表示button上的字在button上的位置。

  可選值[多選時用“|”分開]

    top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

  • top 將對象放在其容器的頂部,不改變其大小.
  • bottom 將對象放在其容器的底部,不改變其大小.
  • left將對象放在其容器的左側,不改變其大小.
  • right將對象放在其容器的右側,不改變其大小.
  • center_vertical 將對象縱向置中,不改變其大小.

  垂直對齊:垂直方向上置中對齊。

    fill_vertical 必要的時候增加對象的縱向大小,以完全充滿其容器. 垂直方向填充

    center_horizontal 將對象橫向置中,不改變其大小水平對齊:水平方向上置中對齊

    fill_horizontal 必要的時候增加對象的橫向大小,以完全充滿其容器. 水平方向填充

    center 將對象橫縱置中,不改變其大小.

    fill 必要的時候增加對象的橫縱向大小,以完全充滿其容器.

    clip_vertical 附加選項,用於按照容器的邊來剪下對象的頂部和/或底部的內容. 剪下基於其縱向對齊設定:頂部對齊時,剪下底部;底部對齊時剪下頂部;除此之外剪下頂部和底部.垂直方向裁剪

  clip_horizontal 附加選項,用於按照容器的邊來剪下對象的左側和/或右側的內容. 剪下基於其橫向對齊設定:左側對齊時,剪下右側;右側對齊時剪下左側;除此之外剪下左      側和右側.水平方向裁剪

  例子

  TextView要讓文本垂直/水平置中顯示,有兩種情況需要考慮:

  1、layout_width/layout_height為wrap_content,此時要讓TextView在父控制項上置中顯示,必須設定layout_gravity=”center”。

  2、layout_width/layout_height為fill_parent,此時由於TextView已佔據父表單所有空間,必須設定gravity=”center”。

二、相對布局

  相對布局的子控制項會根據它們所設定的參照控制項和參數進行相對布局。參照控制項可以是父控制項,也可以是其它子控制項,但是被參照的控制項必須要在參照它的控制項之前定義

  常用屬性:

  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 離某元素上邊緣的距離
    EditText的android:hint
  設定EditText為空白時輸入框內的提示資訊。
    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 用矩陣來繪製,動態縮小放大圖片來顯示。
    ** 要注意一點,Drawable檔案夾裡面的圖片命名是不能大寫的。

例子:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <?xml version="1.0" encoding="utf-8"?> 3 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <AnalogClock 8     android:id="@+id/aclock" 9     android:layout_width="wrap_content"10     android:layout_height="wrap_content"11     android:layout_centerInParent="true" />12 <DigitalClock13     android:id="@+id/dclock"14     android:layout_width="wrap_content"15     android:layout_height="wrap_content"16     android:layout_below="@id/aclock"17     android:layout_alignLeft="@id/aclock"18     android:layout_marginLeft="40px" />19 <TextView20     android:layout_width="wrap_content"21     android:layout_height="wrap_content"22     android:text="目前時間:"23     android:layout_toLeftOf="@id/dclock"24     android:layout_alignTop="@id/aclock"/>25 </RelativeLayout>

 

三、幀布局(FrameLayout)

  幀布局,又叫架構布局,是五大布局中最簡單的一個布局,在這個布局中,整個介面被當成一塊空白備用地區,所有的子項目都不能被指定放置的位置,它們統統放於這塊地區的左上方,並且後面的子項目直接覆蓋在前面的子項目之上,將前面的子項目部分和全部遮擋。顯示效果如下,第一個TextView被第二個TextView完全遮擋,第三個TextView遮擋了第二個TextView的部分位置

 

常用屬性:

  android:foreground:設定改幀版面配置容器的前景映像

  android:foregroundGravity:設定前景映像顯示的位置

 

例子:

  

 1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" android:layout_height="fill_parent"> 4 <LinearLayout android:id="@+id/linearLayout1" 5 android:layout_height="match_parent" 6 android:layout_width="match_parent"> 7 <Button android:text="Button" 8 android:id="@+id/button1" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"></Button>11 </LinearLayout>12 <LinearLayout android:layout_width="match_parent"13 android:id="@+id/linearLayout3"14 android:layout_height="match_parent"15 android:gravity="bottom|right">16 <Button android:text="Button"17 android:id="@+id/button3"18 android:layout_width="wrap_content"19 android:layout_height="wrap_content"></Button>20 </LinearLayout>21 <LinearLayout android:layout_height="match_parent"22     android:id="@+id/linearLayout2"23     android:layout_width="match_parent"24     android:gravity="right">25 <Button android:text="Button"26     android:id="@+id/button2"27     android:layout_width="wrap_content"28     android:layout_height="wrap_content"></Button>29 </LinearLayout>30 <LinearLayout android:layout_width="match_parent"31     android:id="@+id/LinearLayout01"32     android:layout_height="match_parent"33     android:gravity="bottom|left">34 <Button android:id="@+id/Button01"35     android:text="Button"36     android:layout_width="wrap_content"37     android:layout_height="wrap_content"></Button>38 </LinearLayout>39 </FrameLayout>

 

 

四、絕對布局(AbsoluteLayout)

  絕對布局的子控制項需要指定相對於此座標布局的橫縱座標值,否則將會像架構布局那樣被排在左上方。手機應用需要適應不同的螢幕大小,而這種布局模型不能自適應螢幕尺寸大小,所以應用的相對較少。

 

常用屬性:

  1.控制大小

  android:layout_width:組件高度

  android:layout_height:組件高度

  2.控制位置

  android:layout_x:設定組件的X座標

  android:layout_y:設定組件的Y座標

 由於該布局應用低,不同的手機,螢幕大小不同,顯示的布局也不一樣,所以我就部深入瞭解了

五、表格版面配置(TableLayout)

  表格版面配置模型以行列的形式管理子控制項,每一行為一個TableRow的對象,當然也可以是一個View的對象。TableRow可以添加子控制項,每添加一個為一列。

 

常用屬性:

  

  android:collapseColumns:將TableLayout裡面指定的列隱藏,若有多列需要隱藏,請用逗號將需要隱藏的列序號隔開。             

  android:stretchColumns:設定指定的列為可伸展的列,以填滿剩下的多餘空白空間,若有多列需要設定為可伸展,請用逗號將需要伸展的列序號隔開。                

  android:shrinkColumns:設定指定的列為可收縮的列。當可收縮的列太寬(內容過多)不會被擠出螢幕。當需要設定多列為可收縮時,將列序號用逗號隔開。

   列元素(Button)屬性:(奇怪的是button 裡面沒有android:layout_column 和android:layout_span兩個屬性,寫進去無反應,還不知道為什麼)

  android:layout_colum:設定該控制項在TableRow中指定的列。

  android:layout_span:設定該控制項所跨越的列數。

 

例子:

 

  

 1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:stretchColumns="1"> 6 <TableRow> 7 <TextView 8 android:layout_column="1" 9 android:padding="3dip" android:text="Row1"/>10 <TextView11 android:text="1"12 android:gravity="right"13 android:padding="3dip" />14 </TableRow>15 <View16 android:layout_height="2dip"17 android:background="#FF909090" />18 <TableRow>19 <TextView20 android:text="*"21 android:padding="3dip" />22 <TextView23 android:text="Row12"24 android:padding="3dip" />25 <TextView26 android:text="2"27 android:gravity="right"28 android:padding="3dip" />29 </TableRow>30 <View31 android:layout_height="2dip"32 android:background="#FF909090" />33 <TableRow>34 <TextView35 android:layout_column="1"36 android:text="Row13"37 android:padding="3dip" />38 </TableRow>39 </TableLayout>

Android_Layout (一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.