1. 程式人生 > >Android的幾種常用的佈局

Android的幾種常用的佈局

1.Android最常用的佈局主要包括FrameLayout(框架佈局)linerLayout(線性佈局)AbsoluteLayout(絕對佈局)RelativeLayout(相對佈局)以及tableLayout(表格佈局)

其中最為常用的是線性佈局以及相對佈局,這兩種佈局方式能避免產生因為各種的螢幕的解析度的不同而造成的Aciviy顯示變形等各種螢幕適配的問題。

2.一些屬性的區別:比如layout_gravity gravity等的屬性的區別:前者指的是這個空間在父控制元件中所要顯示的位置。後者通常指的是控制元件中的文字內容在該控制元件中所顯式的位置。這裡要注意如下的一種情況。

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="程式管理器"
        android:textColor="#66ff00"
        android:textSize="28sp" />

和第二種情況相似的另外一個情況:padding和margin的區別:

padding是站在父view的角度描述問題,它規定它裡面的內容必須與這個父view邊界的距離。margin則是站在自己的角度描述問題,規定自己和其他(上下左右)的view之間的距離,如果同一級只有一個view,那麼它的效果基本上就和padding一樣了。

3.layout_weight:這個主要指的是控制元件的重要程度,要根據layout_width的屬性而定:比如如下的情況:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"
/>
</LinearLayout>
出現的佈局是:button1佔了2/3,button2佔了1/3。

但是另外一種情況:

但是如果將佈局檔案中的button的屬性android:layout_width="fill_parent"改為android:layout_width="wrap_content"那麼出現的結果為:button1佔了1/3,button2佔了2/3。

具體的實現原理個人也不是很清楚,但是可以死記為如下的規則:

當layout_width:fill_parent的時候,數值小的重要度反而更大,但是如果屬性值為wrap_content的時候,正好相反,但是不管哪種情況,數值大的多的那個佔據的空間一般都比較大。

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                  離某元素上邊緣的距離

關於佈局需要注意如下的兩點:

          注意事項:

1 、各佈局不要亂用各自的屬性。比如把屬於 AbsoluteLayout 佈局的android:layout_x和android:layout_y用到 LinearLayout 佈局或 RelativeLayout 佈局,或者把 RelativeLayout 佈局的 below , rightof 等屬性應用到其他佈局中。這樣做雖然不會報錯,但這是白浪費感情的工作,根本達不到我們需要的效果。

2 、關於android:layout_width="fill_parent" 和 android:layout_height="wrap_content" ,這是對每個佈局寬和高的設定。 wrap_content 可表示隨著其中控制元件的不同而改變這個佈局的寬度或高度,類似於自動設定寬和高, fill_parent 使佈局填充整個螢幕,另外還有一種 match_parent ,它本質上和 fill_parent 一樣,並從 API  Level8 開始替代 fill_parent 。