前言:android
Android應用的UI是由層級的視圖對象組成的。視圖層次起始於一個佈局容器。每一個視圖表明着屏幕上的一塊組成。每一個視圖經過屬性來設定自身在屏幕上顯現的形態。能夠說Android的視圖就是由佈局容器、容器中的子視圖、視圖的各類屬性串聯而成。今天在B哥的指導下 咱們對組成視圖的各個因素一 一加以探究。佈局
1、Android中視圖的表現形式:學習
Android使用XML佈局與Java代碼共同定義視圖(用戶界面)。你可使用Java代碼來制定全部的視圖佈局。但在絕大多數狀況下是使用XML。使用XML能最大限度的利用Android的自動資源選擇特性。在此咱們先只介紹以XML形式定義出來的視圖測試
Android中的每一個界面,都會由不少元素組成。好比一個圖片、一段文字等等。咱們將這些元素按照咱們的意願,而且在Android系統承認的規則下,排布出咱們想要的界面。而提供這些規則的。會是一個容器,這種容器咱們一般稱之爲一個佈局。組成頁面的各個元素,咱們都要將之放入到佈局容器中,而有時佈局自己也是頁面組成的一部分,它也是頁面的組成元素(這裏指的是佈局間的嵌套)。Android提供了5種佈局容器供咱們使用,每一種佈局都有本身的規則。咱們將界面元素放入某種佈局以後。在該佈局中的全部元素都要遵循該佈局所設定的規則(線性、相對、表格等等),在後面的內容裏咱們將一 一加以介紹。字體
這裏咱們還要澄清一個概念,這就是咱們所提到的元素。你們在這裏能夠把元素理解成界面的一個部分,即界面是一個一個元素組成的。元素能夠是一個佈局、一個系統控件、還能夠是一個咱們自定義的控件。spa
若是一個元素(這時通常狀況下 這個元素會是一個佈局)能夠含有、容納,其餘元素。那麼咱們就能夠稱之爲一個父元素,而被它含有的元素,則稱之爲它的子元素。一個頁面元素。能夠同時既是父元素也是子元素(它被別的元素包含、同時它本身也含有另外的元素),這種狀況通常出如今佈局的嵌套中。頁面上的每一個元素,都會經過本身的屬性來控制本身和子元素的位置、形態等等。因此咱們接下來要給你們引入屬性的概念。翻譯
2、Android中的視圖屬性:3d
爲了控制每一個元素在頁面上的展現。Android提供了種類繁多的元素屬性。這些屬性決定了每一個元素的展示形態。不一樣的屬性設置、組合。造就了元素的變幻無窮。而各個元素又組成了咱們的界面。 視圖屬性--->組成Android視圖基因組的最小單位。在XML中進行屬性的設置必需要遵循如下形式:Android:屬性名 = 「屬性值」;xml
3、重要應用屬性對象
這裏咱們選擇幾種全部元素都會遇到且很容易混淆的屬性加以介紹。
² 高和寬
每視圖的高和寬 經過layout_width和layout_height兩個屬性設定,能夠是精確值(XXXdp),能夠是象徵值match_parent\fill_parent(充滿父元素)、wrap_content(佔用足夠用的父元素空間);通常來講match_parent和wrap_content要比精確值靈活得多,其優勢主要體如今象徵值能夠適配任何屏幕尺寸。
注意:match_parent和fill_parent都是充滿父元素。在Android 2.2開始fill_parent更名爲match_parent。fill_parent仍容許向下兼容,若是考慮版本兼容問題,建議使用fill_parent。
² padding和margin
二者都是距離屬性,區別在於margin定義的是元素與元素之間的距離,padding指的是元素裏的內部內容與元素邊界的距離。margin對外,padding對內。
² gravity與layout_gravity
layout_gravity表示的是子元素在父元素中的位置,在這裏必定要注意,是位置而不是距離,layout_gravity的值是上下左右的象徵值而不是以尺寸單位定義的精確值。換言之是指子元素在父元素中的頂部、底部、中部等等。而gravity是以父元素爲視角。來定義本身的子元素,在自身中的位置。兩者定義的都是子元素在父元素中的位置,區別就是layout_gravity是子元素須要定義的屬性,gravity是父元素須要定義的屬性。例如要將一個子元素放置在其父元素的頂部,咱們有兩種方法能夠選擇,一是定義子元素的layout_gravity屬性設置爲top;二是將父元素的gravity屬性設置爲top;
在開發中更傾向於使用layout_gravity屬性,以子元素爲視角只控制自身,這樣更加靈活。由於若是要設置父元素的gravity屬性,那麼其全部子元素都會根據gravity的設定,所有放置在其內部的指定位置;
建議:根據實際開發的經驗來看,要想靈活的控制視圖的位置,咱們須要先以gravity與layout_gravity設置其方位,再以padding和margin gravity調整、微調具體距離,來達到咱們想要的效果;
² Android佈局的尺寸單位
Android絕大多數狀況下采用了一種特殊的尺寸單位。即dp(density-independent),翻譯做 與密度無關像素 簡稱(寫做)dp。1dp=1/160英寸,dp這個單位的意義在於邏輯長度單位隨着密度變化,對應的像素數量也變化,即你能夠根據看到的實際大小,在應用中設置對應的dp值,而使得不一樣的設備上看起來同樣大,從而達到屏幕適配的效果。
Android中使用的尺寸單位有不少,例如 px、sp、pt等等。可是使用dp做爲尺寸單位是Android處理不一樣屏幕尺寸和分辨率的一種方式,這是其餘尺寸單位所不具有的。初學者只須要明白指定UI元素大小的時候你應該使用dp做爲單位。
Android常見長度單位對照表:
單位名稱 |
名稱解釋 |
px |
是屏幕的像素點 |
in |
英寸, 屏幕的物理尺寸, 每英寸等於 2.54 釐米。 |
mm |
毫米 |
pt |
磅,1/72 英寸 |
dp |
邏輯長度單位,在 160 dpi 屏幕上,1dp=1px=1/160英寸。隨着密度變化,對應的像素數量也變化,但並無直接的變化比例。 |
sp |
與dp相似,可是能夠根據用戶的字體大小首選項進行縮放 |
4、Android中的佈局
² FrameLayout
FrameLayout是五大布局中最簡單的一個佈局,在這個佈局中,整個界面被當成一塊空白備用區域,全部的子元素都不能被放置到指定的位置,換言之這個容器(佈局)沒有能夠精確放置子元素到指定位置的方法,它會將它的子元素通通放於這塊自身區域的左上角(能夠經過gravity屬性來移動子元素的位置象徵值),而且後面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和所有遮擋,因此FrameLayout是一個以層次劃分操做的佈局,在XML中具體表現是,先定義(下文代碼中的a1)的子元素會被放置在佈局的最底層,接下來是a2->a4,以用戶的視角來講,他會在界面上最早看到是與他「距離最近」的a4。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/g"
/>
<TextView
android:id="@+id/a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="s1"
android:background="#FF0000"
android:textColor="#000000"
android:textSize="18dip"
/>
<ImageView
android:id="@+id/a3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image"
android:layout_gravity="bottom"
/>
<TextView
android:id="@+id/a4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="s2"
android:layout_gravity="bottom"
/>
</FrameLayout>
² TableLayout
TableLayout是展現表格格式數據的Layout(容器),以行和列的形式對其內部的子元素進行管理,每一行爲一個TableRow對象,或任意一種的子元素。當一行爲一個TableRow元素時,可在TableRow下繼續添加子元素,默認狀況下,每一個子元素佔據一列。當一行爲一個非TableRow元素時,該元素本身獨佔一行。以下圖:
TableLayout的行數由開發人員直接指定,即有多少個TableRow對象(或其餘頁面元素),就有多少行。
TableLayout的列數等於含有最多子元素的TableRow的列數。如一個 TableLayout中有4個TableRow,第一個TableRow含2個子元素,第二個TableRow含3個,第三個TableRow含4個,那麼該TableLayout的列數爲4. 如上圖所示 該TableLayout的列數爲4 由於最上面的TableRow含有4個子元素,而底下的TableRow含有3個子元素.
TableLayout的重要屬性應用。
一、TableLayout的子元素不能指定寬度即layout_width屬性是無效的,具體在咱們的圖中就是S一、S二、S3,此三個元素是不能被設定layout_width屬性的,這個是由TableLayout來處理並設置的,可是他們的子元素是能夠隨意設定layout_width的。
二、全局屬性(列屬性),此類屬性是TableLayout自己根據須要,設定的屬性。
stretchColumns:設置可伸展的列。該列能夠向行方向(橫向)伸展,最多可佔據一整行。
shrinkColumns:設置可收縮的列。當該列子元素的內容太多,已經擠滿所在行,那麼該子元素的內容將往列方(縱向)向顯示。
collapseColumns :設置要隱藏的列。
說明:列能夠同時具有stretchColumns及shrinkColumns屬性,若此,那麼當該列的內容N多時,將「多行」顯示其內容。(這裏不是真正的多行,而是系統根據須要自動調節該行的layout_height)
舉例:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:collapseColumns="2"
android:shrinkColumns="1"
android:stretchColumns="0" >
<TableRow>(第0行)
<Button android:text="第0行,第0列" />(A0)
<Button android:text="第0行,第1列" />(A1)
<Button android:text="第0行,第2列" />(A2)
</TableRow>
<TableRow>(第1行)
<TextView android:text="第1行,第0列 能夠被隱藏 " />(B0)
<TextView android:text="第1行,第1列 能夠很深" />(B1)
</TableRow>
</TableLayout>
在TableLayout中設置了android:collapseColumns="2",第2列能夠被隱藏,因此第0行,第2列被隱藏了。
stretchColumns="0" 第0列能夠被伸展因此當第1行,第0列中內容較多時,自動橫向延伸了
android:shrinkColumns="1"第1列能夠縱向延伸,因此當第1行,第1列內容太多,已經擠滿所在行,那麼該子元素的內容將往列方(縱向)向顯示
三、單元格屬性(列屬性)此類屬性是TableLayout的子元素根據本身須要,設定的屬性
layout_column:指定該元素在第幾列顯示
layout_span:指定該元素佔據的列數(未指定時,爲1)
舉例:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip" >
<TableRow>(第0行)
<Button android:text="第0行,第0列" />
<Button android:text="第0行,第1列" >
<Button android:text="第0行,第2列" />
</TableRow>
<TableRow> (第1行)
<TextView android:text="A1" android:layout_column="1"/>
</TableRow>
<TableRow> (第2行)
<TextView
android:text="A2跨到第2列,不信你看!"
android:layout_column="1"
android:layout_span="2"
/>
</TableRow>
</TableLayout>
第1行 設置了android:layout_column="1",因此A1在第1列開始顯示
第2行 設置了android:layout_column="1" 和android:layout_span="2"因此A2在第1列開始顯示並佔用了兩列。
綜上所述TableLayout雖然在Android佈局中,不是使用率最高的,可是在以表格形式展現數據的應用中仍是蠻好用的,相對於FrameLayout其靈活性已大大增長。不過TableLayout的針對性仍是從某種意義上來講限制了它的使用率,那麼Android有沒有既靈活、通用性又強的佈局呢,答案是確定的,下面咱們就來介紹LinearLayout和RelativeLayout。
² LinearLayout
LinearLayout線性佈局,顧名思義,這個容器是把他的子元素按照垂直或者水平的方向,單向排列,在整個Android的佈局中 LinearLayout是運用最多的佈局之一,可是它也是最好理解的。在LinearLayout中 依靠orientation屬性來設置其子元素的排列方式(水平或垂直)。以後咱們就能夠「傻瓜」式的在LinearLayout排列咱們須要展現的頁面元素了。
縱向展現:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >(排列方向設定)
<TextView android:text="元素1" />(子元素1)
<TextView android:text="元素2" />(子元素2)
<TextView android:text="元素3" />(子元素3)
</LinearLayout>
你們能夠看到 在LinearLayout中 android:orientation="vertical" 將佈局的排列方式指定爲vertical垂直,其子元素就以定義的前後順序,在頁面上垂直排列上來。
反之以下;
將orientation屬性值 設置爲horizontal 子元素橫向排列
android:orientation="horizontal"
LinearLayout中的weight
Weight是LinearLayout特有的一種屬性,經過設置layout_weight屬性,能夠將其子元素沿LinearLayout設置的方向按比例展現。
舉例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView (s1)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight="1(比重爲1)
/>
<TextView (s2)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="2"/>(比重爲2)
<TextView (s3)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="3"(比重爲3)
/>
</LinearLayout>
一、orientation="horizontal" 整個佈局橫向排布
二、s1-> s3 的比重分別爲一、二、3,因此在視圖上三個子元素在橫向上按1:2:3顯示
在此介紹一下 比重的計算方法 在此實例中LinearLayout有三個子元素,比重值分別設置爲一、二、3,因此整個頁面就會被平均分紅1+2+3=6份 而後按照各個子元素設定weight值的多少,來佔用整個頁面6份中的多少
三、爲何說weight屬性是將其子元素沿LinearLayout設置的方向按比例展現
咱們看一下下面的實例。
在上一段代碼的基礎上 整個佈局爲橫向排布,咱們爲每個子元素的高設定一個精確值
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView (s1)
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="1"
android:layout_weight="1(比重爲1)
/>
<TextView (s2)
android:layout_width="wrap_content"
android:layout_height="30dP"
android:text="2"
android:layout_weight="2"/>(比重爲2)
<TextView (s3)
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="3"
android:layout_weight="3"(比重爲3)
/>
</LinearLayout>
因此weight只在LinearLayout設置的方向上會生效 在另外一方向上是失效的。(例如,在水平方向按weight設定值顯示,那麼在豎直方向上height屬性是不受影響的,反之在豎直方向上按weight設定值顯示,那麼水平方向width屬性不受影響)。
那麼你們肯能會問了 那麼我在LinearLayout設置的方向設置子元素的長或寬,同時又設定了weight,此時weight會不會失效呢,咱們繼續往下看
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView (s1)
android:layout_width="50dp"
android:text="1"
android:layout_weight="1(比重爲1)
/>
<TextView (s2)
android:layout_width="30dp"
android:text="2"
android:layout_weight="2"/>(比重爲2)
<TextView (s3)
android:layout_width="20dp"
android:text="3"
android:layout_weight="3"(比重爲3)
/>
</LinearLayout>
咱們看到在佈局設定的方向上weight屬性是覆蓋了對應的height或width。可是這個是否是就是結論呢,B哥能夠負責的告訴你們 不必定是。咱們繼續看代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView (s1)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11111111111111"
android:layout_weight="1(比重爲1)
/>
<TextView (s2)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="2"/>(比重爲2)
<TextView (s3)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="3"(比重爲3)
/>
</LinearLayout>
設定好weight值後,看一下頁面效果
這下你們迷惑了吧,爲何weight會失效呢?
那是由於:
1佈局自己的排布方向是horizontal,與其方向一致的屬性layout_width = wrap_content
2子元素內容超出其所佔比例空間所能承受的「面積」,在這個時候weight會失效。
只有在這兩個條件都知足的前提下weight會失效。那麼怎麼解決這個問題呢,android也給出了答案:將layout_width設置爲0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView (s1)
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="11111111111111"
android:layout_weight="1(比重爲1)
/>
<TextView (s2)
android:layout_width="0dp "
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="2"/>(比重爲2)
<TextView (s3)
android:layout_width="0dp "
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="3"(比重爲3)
/>
</LinearLayout>
總結:
1:經過設置layout_weight屬性,能夠將其子元素沿LinearLayout設置的方向按比例展現。
2:weight只在LinearLayout設置的方向上會生效 在另外一方向上是失效的。
3:在必定條件下(上文已經介紹),weight會受到width或height的干擾,若是使用weight,把對應的width或height設爲0dp,這樣一來視圖的大小就會只授權值的控制而不會受到干擾。
4:在不設置weight屬性的時候,該屬性默認爲0。
介紹完LinearLayout 你們是否是以爲 它比以前給你們介紹的全部佈局都要靈活了呢。可是在android應用界面的開發中咱們每每須要一種更靈活的佈局方式,爲此RelativeLayout呼之欲出。
² RelativeLayout
RelativeLayout相對佈局 是比LinearLayout線性佈局更加靈活的一種佈局。佈局中元素的位置是按照相對位置來計算的,後一個元素在什麼位置依賴於前一個元素的基本位置,換言之RelativeLayout 容許子元素指定它們相對於其父元素或兄弟元素的位置,這是實際佈局中最經常使用的佈局方式之一。它靈活性大不少,固然屬性也多,操做難度也大,屬性之間產生衝突的的可能性也大,使用相對佈局時要多作些測試。
RelativeLayout的操做全在於佈局內部每一個元素經過自身屬性的設定組合來在RelativeLayout中展現一個咱們想要的界面。因此在RelativeLayout中特有的屬性,也是全部佈局中最多的,咱們給RelativeLayout的各個子元素,設定各類屬性。來控制他們在佈局中的位置。從而達到咱們想要的效果。 下面咱們就來介紹一下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 離某元素上邊緣的距離
咱們仍是經過例子來學習以上屬性
咱們在一個RelativeLayout中分別放置s1-s9 ,9個子元素。下面咱們就來講說這9個子元素是如何別放置到他們的父容器,一個RelativeLayout中去的。
一、s1:咱們根據截圖能夠看出s1是被放置在RelativeLayout的左上角的。Android佈局中沒有哪一個屬性是能夠直接將元素放置在佈局的哪一個角上的。因此咱們要曲線救國,兩步走。
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
即s1 即在父元素的頂部,又在父元素的左邊,其效果就是s1出如今父元素的左上角
同理:
S2右上角:
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
S3左下角:
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
S4右下角:
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
好如今問題出來了,若是某個頁面元素我既想放置在一個RelativeLayout邊角位置,又想和邊界有一些距離,這種效果怎麼達到。通常這種狀況下咱們會啓用一開始咱們介紹過的margin屬性。
還拿s1舉例
咱們在android:layout_alignParentTop="true" android:layout_alignParentLeft="true"的基礎上加上android:layout_marginTop="10dp"android:layout_marginLeft="15dp"
來微調元素與邊界之間的距離從而達到咱們想要的效果。
說完四邊 咱們來講說怎麼讓一個元素被放置在RelativeLayout的中間
咱們來看S5
android:layout_centerInParent="true" 相對於父元素徹底居中。
相對佈局的相對指的是 後一個元素在什麼位置依賴於前一個元素的基本位置。那麼S5以後的S6的位置就要相對於S5說了
android:layout_above="@id/s5"
android:layout_marginBottom="15dp",
在s5的上面,而且距離底部元素s5 15個dp 這裏要着重注意一下,layout_above是在XXX的上面,marginBottom是距離底部的元素多少距離。一目瞭然,位置的屬性above等是以目標元素爲視角。距離屬性margin是以自身爲視角。理解了這層關係,你對RelativeLayout的理解會更加深入。
同理:
S7:android:layout_below="@id/s5"
android:layout_marginTop="15dp"
S8:android:layout_toLeftOf="@id/s5"
android:layout_marginRight="15dp"
S9:android:layout_toRightOf="@id/s5"
android:layout_marginLeft="15dp"
接下來咱們還有兩個比較重要的屬性要介紹這就是android:layout_centerHrizontal和android:layout_centerVertical 其實也很好理解。在這裏給你們一個公式。
layout_centerInparent= layout_centerHrizontal+ layout_centerVertical
layout_centerInparent指的是在父元素的正中心
layout_centerHrizontal是指在父元素的水平中心,在豎直方向上,默認爲layout_alignParentTop
layout_centerVertical
是指在父元素的豎直中心,水平方向上,默認爲layout_alignParentLeft
以上介紹了RelativeLayout的經常使用屬性,你們在應用時每每會遇到這麼一個問題,那是屬性間有衝突的現象。固然具體問題要具體分析,這裏給你們一個方向,那就是相對於父元素操做的屬性alignParentXXX和centerInparent。。。等等的屬性,其優先級每每要大於對同級元素操做的屬性如咱們介紹的第二類屬性屬性值必須爲id的引用名「@id/id-name」。
在B哥多年的經驗下 給你們一個操做RelativeLayout的大致思路,先考慮目標爲父元素的屬性設定,在細化到對兄弟元素的位置關係的屬性設定。先設定大致位置,再用距離進行微調。對齊要用一個方向的目標元素,切忌不可循環依賴。
AbsoluteLayout:
相對於RelativeLayout,android還提供了一個佈局名爲 絕對佈局AbsoluteLayout,可是在B哥教導下,給你們說一下這個雞肋的絕對佈局。該佈局是不會寫代碼的人專用的,直接在ADT下拉組件到合適位置便可;很坑爹的說!絕對佈局是直接經過X,Y座標來控制組件在佈局中的位置,這是它惟一的特色。說真的,這佈局如今真的不多用,由於手機的屏幕大小與分辨率都千差萬別,極可能你在虛擬機上是居中的,而後一換你手機跑就錯位了,所以使用AbouluteLayout並非一個明智的選擇。
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"" >
<TextView
android:id="@+id/txt1"
android:text="絕對佈局的使用"
android:layout_x="80px"
android:layout_y="80px"/>
<TextView
android:text="2.3以後廢除了"
android:layout_x="120px"
android:layout_y="120px"/>
<ImageView
android:layout_x="80px"
android:layout_y="180px"
android:src="@drawable/ic_launcher"/>
</AbsoluteLayout>
這裏惟一要說明的 在絕對佈局中起始點0,0爲左上角,其座標值依次增長到佈局中的右下角,直至將座標點「推出」屏幕。
以上圖代碼爲例txt1的layout_x和layout_y均爲80px,也就是說 它在佈局中的位置是以左上角爲基準,向右平移80px,再向下豎移80px爲本身的出現位置