LinearLayout 線性布局特性:
橫向或縱向,每行或每列(根據方向)只能有1個對象,可以設定對象在LinearLayout中佔位的權重。
看上去很簡單,也確實很簡單,但LinearLayout卻是Android中使用最多的布局之一,尤其是在多個Linear嵌套使用的時候,就會顯得異乎尋常的強大。
例子:
1234567891011121314151617181920212223242526272829 |
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:text="Title" android:textSize="18dp" android:background="#AA3333FF"android:layout_width="fill_parent" android:layout_height="wrap_content"/><ListViewandroid:id="@+id/listViewInLinear"android:layout_width="fill_parent" android:layout_height="wrap_content"android:paddingTop="5dp" android:paddingBottom="5dp"android:layout_weight="1"/><LinearLayoutandroid:layout_width="fill_parent" android:layout_height="wrap_content"android:orientation="horizontal"><Button android:id="@+id/show"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Show" android:layout_weight="50"/><Button android:id="@+id/back"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Back" android:layout_weight="50"/></LinearLayout></LinearLayout> |
下面是onCreate中ListView資料填充的方法,直接使用了APIDemo中的資料,用最簡單的ArrayAdapter來實現。
1234567 |
ListView arrowList=(ListView) findViewById(R.id.listViewInLinear);arrowList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings));//mString定義 private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", ...} |
下面是運行,可以看到簡單的2個LinearLayout嵌套外加layout_weight的使用,構造各解析度自適應的布局是如此簡單。
注意:同1個LinearLayout中可以設定多個子組件都有layout_weight屬性,此時它們的大小會按weight的比例來進行分配,一般建議如果有多個weight,可以考慮讓它們的和等於100。
另外,如果設定了layout_weight,那麼與LinearLayout相同方向的大小設定不要設定fill_parent,而是設定wrap_content比較好,否則會發生問題。(具體情形遇見便知)
關鍵字:
orientation(方向),layout_weight(權重),嵌套
RelativeLayout 相對位置布局特性:
布局內對象的位置可以相對於Layout本身來定位,也可以根據之前的對象來定位。非常適合用來表現多個對象對齊(或者故意不對齊?)的布局。
上面這段描述像不像什麼都沒說?其實RelativeLayout可以做的事情基本上都可以由多個LinearLayout來實現,但在條件適合的時候用RelativeLayout來實現會更方便,更強大。而且有些情況下幾乎只有RelativeLayout才能實現了。
考慮以下這個布局的實現方式,
布局的第1行其實完全可以用LinearLayout來實現,只需要添加1個無文本的TextView在伸展區,並且設定layout_weight即可,雖然有些麻煩,但仍然可以實現。但第2行如何?呢?要求是新的按鈕與Right1靠右對齊。而用RelativeLayout就可以輕鬆的實現,下面是Layout定義檔案:
例子:
12345678910111213141516171819202122 |
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><Button android:id="@+id/relaLeftButton" android:text="Left"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/><Button android:id="@+id/relaRight2Button" android:text="Right2"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignParentRight="true" android:layout_alignParentTop="true"/><Button android:id="@+id/relaRight1Button" android:text="Right1"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignTop="@id/relaRight2Button" android:layout_toLeftOf="@id/relaRight2Button"/><Button android:id="@+id/relaRight3Button" android:text="Right3Long"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignRight="@id/relaRight1Button" android:layout_below="@id/relaRight1Button"/></RelativeLayout> |
在你使用了RelativeLayout之後,你會發現其中的控制項都多了很多的layout_align開頭的屬性,有用於與父視窗對齊的,有用於和其它控制項對齊和聲明擺放位置的。如此便可以輕鬆的實現相對布局。下面是運行時:
是不是達成要求了?雖然這是1個比較極端的例子,但在實際應用中,RelativeLayout的使用還是相當普遍的,通常用於登入畫面之類的,元素不多,但要求布局相對寬鬆盡量利用空間的情況下。