標籤:android線性布局linearlay
一、先瞭解幾個屬性:
1.布局
1) android:orientation="vertical" 垂直布局
2)android:orientation="horizontal" 水平布局
2.控制項內容高和寬布局
1) android:layout_width="match_parent" ,布滿整個螢幕. [匹配父視窗]
2)android:layout_height="wrap_content",布局元素將根據內容更改大小.[內容包括]
3.布局比例
所有的視圖都有一個layout_weight值,預設為零,意思是需要顯示 多大的視圖就佔據多大的螢幕空 間。若賦一個高於零的值,則將父視 圖中的可用空間分割,分割大小具體取決於每一個視圖的layout_weight 值以及該值在當前螢幕布局的整體 layout_weight值和在其它視圖螢幕布 局的layout_weight值中所佔的比率而定。
二、下面兩個例子實現:
1)三個按鈕橫向擺放
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="mc" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="m+" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="m-" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="mr" /> </LinearLayout>
借用layout_weight使三個按鈕擺放佔用比例一致.
二、兩層按鈕:第一層三個,第二次2個
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="3" android:orientation="horizontal" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="3" android:orientation="horizontal" > <Button android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="2" android:text="0" /> <Button android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" android:text="." /> </LinearLayout> </LinearLayout>
先用一個大層,設定權重比例都是3. 第二層的第一個按鈕比例的權重又占它屬於層的2/3.
Android線性布局LinearLayout(七)