編寫android視圖有兩種方式可以實現
1,通過配置xml來實現
2,通過動態編碼來實現
通過第一種方式可以將視圖和商務邏輯很好的分離開來,也是使用比較多的
第二種情況一般在需要動態改變頁面配置的情況下使用
android的View的設計主要使用了java的組合模式
組合模式:將對象以樹形結構組織起來,以達成“部分-整體” 的階層,使得用戶端對單個對象和組合對象的使用具有一致性.
按照組合模式的定義,這裡的單個對象可以具體到android的每一個基本組件(Button,CheckBox,TextView等等)
而組合對象是指那些實現了ViewGroup介面的對象
在android中,組合對象控制其內部所有單個對象的布局
有些組合對象本身也是UI組件可以拿來直接使用,如GridView,Spinner,ListView等等
而也有一些組合對象它們本身並不是UI組件,只是用於控制其內部所有單個對象的布局,也就是android中的布局對象
android中比較常用的布局有LinearLayout,TableLayout和Relativelayout
LinearLayout:
主要屬性
orientation:horizontal|vertical用來表示是水平布局還是垂直布局
layout_weight:相對於其他組件該組件的空間分配比率
假設水平布局,且有3個組件
如果3各組件的layout_width全部為wrap_content,layout_weight為1、2、1,
則第二個組件的水平空間是第一個和第3個的空間之和
TableLayout
關鍵屬性stretchColumns
android:stretchColumns="1,2"
stretchColumns屬性用來指定哪一個column具備自動調整功能來匹配表格的寬度
表格的行
最常使用的是TableRow,用來表示表格的一行
除了使用TableRow我們也可是使用其他的View視圖來作為TableLayout的children節點,那麼這個視圖將作為單獨的一行存在,並佔據著所有的column
註:TableLayout的children節點不能指定layout_width屬性,它的值永遠都是 FILL_PARENT,但我們可以指定layout_height屬性(預設值是wrap_content)
而Linerlayout的children節點layout_width屬性和layout_height是必須要指定的
表格的列column
表格的哪一行的儲存格數(cell)最多,那麼該表格的列數就是該行的儲存格數
表格的儲存格cell
每一個TableRow的children節點都代表一個cell,相當於html標籤裡的td
但與html不同的是,在TableLayout裡我們不能讓一個cell佔有兩個或兩個以上的column
就像html中的<td colspan="2"> 這種格式無法實現
另外我們可以對每一個cell指定layout_column屬性,用來表示該cell是顯示在哪一個column中,column的起始計數是從0開始的
RelativeLayout
某一個組件的位置是相對於其他組件的位置來決定的
在與某個組件的比對過程中,所比對的組件是通過(@id/id)的方式來進行引用的,
因此在xml中聲明新組件之前,應當首先聲明與它進行比對的組件id
RelativeLayout所定義的布局規則rules
在xml中都是以layout_attribute的格式,這裡唯寫出attribute
above aligns a child's bottom edge with another child's top edge
below aligns a child's top edge with another child's bottom edge
alignTop aligns a child's top edge with another child's top edge
alignBottom aligns a child's bottom edge with another child's bottom edge
alignBaseline aligns a child's baseline with another child's baseline
alignLeft aligns a child's left edge with another child's left edge
alignRight aligns a child's right edge with another child's right edge
alignParentLeft aligns the child's left edge with its RelativeLayout parent's left edge true|false
alignParentRight aligns the child's right edge with its RelativeLayout parent's right edge true|false
alignParentTop aligns the child's top edge with its RelativeLayout parent's top edge true|false
alignParentBottom aligns the child's bottom edge with its RelativeLayout parent's bottom edge true|false
centerHorizontal centers the child horizontally with respect to the bounds of its RelativeLayout parent true|false
centerVertival centers the child vertically with respect to the bounds of its RelativeLayout parent true|false
centerInParent centers the child with respect to the bounds of its RelativeLayout parent true|false
toLeftOf aligns a child's right edge with another child's left edge
toRightOf aligns a child's left edge with another child's right edge
padding[Bottom|Left|Right|Top]=value
margin[Bottom|Left|Right|Top]=value
相比LinerLayout和TableLayout,RelativeLayout的構造最為複雜,功能也較為強大,缺點是可能會增加很多不必要的組件id來加大系統開銷