Android佈局管理器 - 詳細解析佈局實現

佈局管理器都是以ViewGroup爲基類派生出來的; 使用佈局管理器可以適配不同手機屏幕的分辨率,尺寸大小;


佈局管理器之間的繼承關係 : 




在上面的UML圖中可以看出, 絕對佈局 幀佈局 網格佈局 相對佈局 線性佈局直接繼承ViewGroup,表格佈局繼承的LinearLayout;


一. 線性佈局(LinearLayout)


1. 線性佈局作用 


作用 : 線性佈局會將容器中的組件一個一個排列起來, LinearLayout可以控制組件 橫向 或者 縱向 排列, 通過android:orientation屬性控制;

不換行屬性 : 線性佈局中的組件不會自動換行, 如果組件一個一個排列到盡頭之後, 剩下的組件就不會顯示出來;


2. LinearLayout常用屬性


(1)基線對齊


xml屬性 : android:baselineAligned

設置方法 : setBaselineAligned(boolean b)

作用 : 如果該屬性爲false, 就會阻止該佈局管理器與其子元素的基線對齊;


(2)設分隔條 


xml屬性 : android:divider

設置方法 : setDividerDrawable(Drawable)

作用 : 設置垂直佈局時兩個按鈕之間的分隔條;


(3)對齊方式(控制內部子元素)  


xml屬性 : android:gravity

設置方法 : setGravity(int)

作用 : 設置佈局管理器內組件(子元素)的對齊方式

支持的屬性 : 

top, bottom, left, right, 

center_vertical(垂直方向居中), center_horizontal(水平方向居中),

fill_vertical(垂直方向拉伸), fill_horizontal(水平方向拉伸), 

center, fill, 

clip_vertical, clip_horizontal; 

可以同時指定多種對齊方式 : 如 left|center_vertical 左側垂直居中;


(4)權重最小尺寸 


xml屬性 : android:measureWithLargestChild

設置方法 : setMeasureWithLargestChildEnable(boolean b);

作用 : 該屬性爲true的時候, 所有帶權重的子元素都會具有最大子元素的最小尺寸;


(5) 排列方式


xml屬性 : android:orientation;

設置方法 : setOrientation(int i);

作用 : 設置佈局管理器內組件排列方式, 設置爲horizontal(水平),vertical(垂直), 默認爲垂直排列;


3. LinearLayout子元素控制


LinearLayout的子元素, 即LinearLayout中的組件, 都受到LinearLayout.LayoutParams控制, 因此LinearLayout包含的子元素可以執行下面的屬性.


(1) 對齊方式


xml屬性 : android:layout_gravity;

作用 : 指定該元素在LinearLayout(父容器)的對齊方式;


(2) 所佔權重


xml屬性 : android:layout_gravity;

作用 : 指定該元素在LinearLayout(父容器)中所佔的權重;


4. 控制子元素排列與在父元素中排列


控制本身元素屬性與子元素屬性 : 帶layout的屬性是設置本身, 例如 android:layout_gravity設置的是本身的對其方式; 不帶layout的屬性是設置其所包含的子元素, 例如android:gravity 設置的是該容器子組件的對齊方式;

所有的佈局管理器都提供了相應的LayoutParams內部類, 這些內部類用於控制佈局中子元素的佈局, 如 對齊方式 layout_gravity, 所佔權重 layout_weight, 這些屬性用於設置本元素在父容器中的對齊方式;


android:gravity作用是指定指定本元素包含的子元素的對齊方式, 只有容器才支持這個屬性;


5. 常見用法


(1) 獲取LinearLayout的寬高


a. 組件外無法獲取組件寬高 


調用View.getHeight() 和 View.getWidth()方法 是獲取不到組件的寬度和高度的, 這兩個方法返回的是0; Android的運行機制決定了無法在組件外部使用getHeight()和getWidth()方法獲取寬度和高度, 在自定義的類中可以在View的類中通過調用這兩個方法獲取該View子類組件的寬和高;


b. 組件外部獲取View對象寬高方法 


使用View.getMeasuredWidth() 和 View.getMeasuredHeight()方法可以獲取組件的寬和高, 在調用這個方法之前, 必須先調用View.measure()方法, 纔可以, 否則也獲取不到組件的寬高;


注意 : 如果組件寬度或高度設置爲fill_parent, 使用getMeasuredHeight()等方法獲取寬度和高度的時候, 並且組件中含有子元素時, 所獲取的實際值是這些組件所佔的最小寬度和最小高度.


示例:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. View view = getLayoutInflater().inflate(R.layout.main, null);  
  2. LinearLayout layout = (LinearLayout) view.findViewById(R.id.linearlayout);  
  3. //調用測量方法, 調用了該方法之後才能通過getMeasuredHeight()等方法獲取寬高  
  4. layout.measure(00);  
  5. //獲取寬度  
  6. int width = layout.getMeasuredWidth();  
  7. //獲取高度  
  8. int height = layout.getMeasuredHeight();  



c. 獲取佈局文件中組件的寬高 


調用View.getLayoutParams().width 和 View.getLayoutParams().height 獲取寬高, 如果寬高被設定爲 fill_parent, match_parent, warp_content 時, 這兩個兩邊直接回返回 FILL_PARENT, MATCH_PARENT, WARP_CONTENT常量值;


(2) 在LinearLayout中添加分隔線


a. 使用ImageView添加(低版本3.0以下)


如果佈局是vertical, 那麼設置一個ImageView寬度fill_parent, 高度2dp, 設置一個背景色;

如果佈局時horizontal, 那麼設置一個ImageView寬度2dp, 高度fill_parent, 設置一個背景色;


[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <ImageView   
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="2dp"  
  4.     android:background="#F00"/>  


b. 使用xml屬性添加(3.0以上版本)


設置LinearLayout標籤的 android:showDividers 屬性, 該屬性有四個值 : 

none : 不顯示分隔線;

beginning : 在LinearLayout開始處顯示分隔線;

middle : 在LinearLayout中每兩個組件之間顯示分隔線;

end : 在LinearLayout結尾處顯示分隔線;


設置android:divider屬性, 這個屬性的值是一個Drawable的id;


c. 使用代碼添加(3.0以上版本)


設置顯示分隔線樣式 : linearLayout.setShowDividers(), 設置android:showDividers屬性;

設置分隔線圖片 : linearLayout.setDividerDrawable(), 設置android:divider屬性;


6. 實際案例


(1) 按鈕排列 


       


要點 : 

左邊的LinearLayout的android:gravity 屬性爲 bottom|center_horizontal; 

右邊的LinearLayout的android:gravity 屬性爲 right|center_vertical;


代碼 : 


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"   
  6.     android:gravity="bottom|center_horizontal">  
  7.     <Button   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="按鈕1"/>  
  11.     <Button   
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="測試按鈕2"/>  
  15.     <Button   
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="按鈕3"/>  
  19.     <Button   
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="測試按鈕4"/>  
  23.     <Button   
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="按鈕5"/>  
  27. </LinearLayout>  

通過修改 android:gravity 屬性來控制LinearLayout中子元素的排列情況;

左邊的圖的屬性爲 bottom|center_horizontal , 右邊的android:gravity的屬性值爲 right|center_vertical;


(2) 三個按鈕各自對齊


三個水平方向的按鈕, 分別左對齊, 居中對齊, 右對齊 :




要點 : 

最頂層的LinearLayout的orientation是horizontal水平的;

第二層的LinearLayout的orientation是vertical垂直的, 並且寬度是fill_parent , 依靠權重分配寬度;

按鈕的android:layout_gravity屬性根據需求 left, center, right, 默認爲left;


代碼 : 


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal"  >  
  6.      
  7.     <LinearLayout   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_weight="1"  
  10.         android:layout_height="wrap_content"  
  11.         android:orientation="vertical"  
  12.         android:background="#f00">  
  13.         <Button android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="按鈕1"/>  
  16.     </LinearLayout>  
  17.       
  18.     <LinearLayout   
  19.         android:layout_width="fill_parent"  
  20.         android:layout_weight="1"  
  21.         android:layout_height="wrap_content"  
  22.         android:orientation="vertical"  
  23.         android:background="#0f0">  
  24.         <Button android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:text="按鈕2"  
  27.             android:layout_gravity="center"/>  
  28.     </LinearLayout>  
  29.       
  30.     <LinearLayout   
  31.         android:layout_width="fill_parent"  
  32.         android:layout_weight="1"  
  33.         android:layout_height="wrap_content"  
  34.         android:orientation="vertical"  
  35.         android:background="#00f">  
  36.         <Button android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:text="按鈕3"  
  39.             android:layout_gravity="right"/>  
  40.     </LinearLayout>  
  41.       
  42. </LinearLayout>  


二. 相對佈局RelativeLayout


相對佈局容器中, 子組件的位置總是相對兄弟組件, 父容器來決定的;


1. RelativeLayout支持的屬性


(1) 對齊方式


xml屬性 : android:gravity;

設置方法 : setGravity(int);

作用 : 設置佈局容器內子元素的對齊方式;


(2) 忽略對齊方式


xml屬性 : android:ignoreGravity;

設置方法 : setIgnoreGravity(int);

作用 : 設置該組件不受gravity屬性影響, 因爲gravity屬性影響容器內所有的組件的對齊方式, 設置了之後, 該組件就可以例外;


2. LayoutParams屬性


(1) 只能設置boolean值的屬性


這些屬性都是相對父容器的, 確定是否在父容器中居中(水平, 垂直), 是否位於父容器的 上下左右 端;


是否水平居中 : android:layout_centerHorizontal;

是否垂直居中 : android:layout_centerVertical;

是否位於中央 : android:layout_centerInParent;


是否底端對齊 : android:layout_alignParentBottom;

是否頂端對齊 : android:layout_alignParentTop;

是否左邊對齊 : android:layout_alignParentLeft;

是否右邊對齊 : android:layout_alignParentRight;


(2) 只能設置其它組件id的屬性


位於所給id組件左側 : android:layout_toLeftOf;

位於所給id組件右側 : android:layout_toRightOf;

位於所給id組件的上邊 : android:layout_above;

位於所給id組件的下方 : android:layout_below;


與所給id組件頂部對齊 : android:layout_alignTop;

與所給id組件底部對齊 : android:layout_alignBottom;

與所給id組件左邊對齊 : android:layout_alignLeft;

與所給id組件右邊對齊 : android:layout_alignRight;


3. 梅花布局效果 


五個按鈕排成梅花形狀, 梅花處於正中心, 效果圖如下 :


 


兩個按鈕, 如果只有 android:layout_above="@+id/bt1" 會是這種情況 : 


加上 android:layout_alignLeft="@+id/bt1"就會成爲這種情況 : 




要點 : 


注意每個組件的屬性, 先要確定方位, 在進行對齊, 組件左邊界對齊, 組件上邊界對齊;


代碼 : 


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.       
  6.     <Button   
  7.         android:id="@+id/bt1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="按鈕1"  
  11.         android:layout_centerInParent="true"/>  
  12.       
  13.     <Button   
  14.         android:id="@+id/bt2"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="按鈕2"  
  18.         android:layout_above="@+id/bt1"  
  19.         android:layout_alignLeft="@+id/bt1"/>  
  20.       
  21.     <Button   
  22.         android:id="@+id/bt3"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:text="按鈕3"  
  26.         android:layout_centerInParent="true"  
  27.         android:layout_below="@+id/bt1"  
  28.         android:layout_alignLeft="@+id/bt1"/>  
  29.       
  30.     <Button   
  31.         android:id="@+id/bt4"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="按鈕4"  
  35.         android:layout_centerInParent="true"  
  36.         android:layout_toLeftOf="@+id/bt1"  
  37.         android:layout_alignTop="@+id/bt1"/>  
  38.       
  39.     <Button   
  40.         android:id="@+id/bt5"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:text="按鈕5"  
  44.         android:layout_centerInParent="true"  
  45.         android:layout_toRightOf="@+id/bt1"  
  46.         android:layout_alignTop="@+id/bt1"/>  
  47.       
  48. </RelativeLayout>  



4. 相對佈局常用方法

(1) 獲取屏幕中一個組件的位置


要先創建一個整型數組, 數組大小2位; 這個數組傳入getLocationOnScreen()方法;

可以調用View.getLocationOnScreen()方法, 返回的是一個數組 int[2], int[0] 是橫座標, int[1] 是縱座標;


[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. //獲取組件  
  2. Button b = (Button) this.findViewById(R.id.Button01);  
  3. //創建數組, 將該數組傳入getLocationOnScreen()方法  
  4. int locations[] = new int[2];  
  5. //獲取位置信息  
  6. b.getLocationOnScreen(locations);  
  7. //獲取寬度  
  8. int width = locations[0];  
  9. //獲取高度  
  10. int height = locations[1];  


(2) LayoutParams的使用設置所有屬性


屬性設置方法少 : Android SDK中View類只提供了很少用於設置屬性的方法, 大多數屬性沒有直接對應的獲得和設置屬性值的方法, 看起來貌似不是很好用;

使用LayoutParams設置屬性值 : Android中可以對任何屬性進行設置, 這裏我們需要一個LayoutParams對象, 使用這個LayoutParams.addRule()方法, 可以設置所有組件的屬性值; 設置完之後調用View.setLayoutParams()方法, 創建剛纔的LayoutParams對象, 並更新View的相應的屬性值;


代碼中動態設置佈局屬性 : 

a. 創建LayoutParams對象

b. 調用LayoutParams對象的addRule()方法設置對應屬性;

c. 調用View.setLayoutParams()方法將設置好的LayoutParams對象設置給組件;

d. 調用addView方法將View對象設置到佈局中去;


使用代碼設置android:layout_toRightOf 和 android:layout_below屬性 : 


[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. //裝載佈局文件  
  2. RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.relative, null);  
  3. //裝載要動態添加的佈局文件  
  4. Button button = (Button) relativeLayout.findViewById(R.id.bt1);  
  5. //創建一個LayoutParams對象  
  6. LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  7. //設置android:layout_toRightOf屬性  
  8. layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.bt2);  
  9. //設置android:layout_below  
  10. layoutParams.addRule(RelativeLayout.BELOW, R.id.bt2);  
  11. //更新Button按鈕的屬性  
  12. button.setLayoutParams(layoutParams);  
  13. //向佈局中動態添加按鈕  
  14. relativeLayout.addView(button);  



三. 幀佈局FrameLayout

幀佈局容器爲每個組件創建一個空白區域, 一個區域成爲一幀, 這些幀會根據gravity屬性自動對齊;


(1) 繪製霓虹燈佈局


繪製一個霓虹燈效果的層疊佈局, 如下圖 : 




要點 : 

後擋前 : 後面的View組件會遮擋前面的View組件, 越在前面, 被遮擋的概率越大;

界面居中 : 將所有的TextView組件的對齊方式 android:layout_gravity 設置爲center;

正方形 : 所有的TextView都設置android:height 和 android:width 屬性, 用來設置其寬高, 這裏設置成正方形, 寬高一樣, 後面的組件比前面的邊長依次少40;

顏色 : 每個TextView的背景都設置成不一樣的;


代碼 : 


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.       
  6.     <TextView   
  7.         android:id="@+id/tv_1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_gravity="center"  
  11.         android:width="320px"  
  12.         android:height="320px"  
  13.         android:background="#f00"/>  
  14.     <TextView   
  15.         android:id="@+id/tv_2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_gravity="center"  
  19.         android:width="280px"  
  20.         android:height="280px"  
  21.         android:background="#0f0"/>  
  22.     <TextView   
  23.         android:id="@+id/tv_3"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_gravity="center"  
  27.         android:width="240px"  
  28.         android:height="240px"  
  29.         android:background="#00f"/>  
  30.     <TextView   
  31.         android:id="@+id/tv_4"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_gravity="center"  
  35.         android:width="200px"  
  36.         android:height="200px"  
  37.         android:background="#ff0"/>  
  38.     <TextView   
  39.         android:id="@+id/tv_5"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_gravity="center"  
  43.         android:width="160px"  
  44.         android:height="160px"  
  45.         android:background="#f0f"/>  
  46.     <TextView   
  47.         android:id="@+id/tv_6"  
  48.         android:layout_width="wrap_content"  
  49.         android:layout_height="wrap_content"  
  50.         android:layout_gravity="center"  
  51.         android:width="120px"  
  52.         android:height="120px"  
  53.         android:background="#0ff"/>  
  54.   
  55. </FrameLayout>  


2. 使用代碼使上面的霓虹燈效果動起來


(1) 圖片效果 




(2) 顏色資源


創建顏色資源, 在跟節點<resources>下面創建<color>子節點, color屬性標籤 name 自定義, 子文本爲顏色代碼;


(3) 定時器控制handler


創建Handler對象, 實現handleMessage()方法, 在這個方法中循環設置 TextView對象的顏色變量, 使用color[(i + currentColor)%colors.length]每調用一次, 就將所有的TextView顏色依次調換一次;

在onCreate()方法中, 開啓一個定時器Timer, 每隔0.2s就調用一個handler中的方法, 這樣動態的霓虹燈代碼就顯示出來了.


(4) 代碼


顏色資源代碼 : 


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name = "color1">#f00</color>  
  4.     <color name = "color2">#0f0</color>  
  5.     <color name = "color3">#00f</color>  
  6.     <color name = "color4">#ff0</color>  
  7.     <color name = "color5">#f0f</color>  
  8.     <color name = "color6">#0ff</color>  
  9. </resources>  


代碼 : 


[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package com.example.framelayout;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Message;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     //這個變量用來控制霓虹燈顏色變化  
  15.     private int currentColor = 0;  
  16.     //顏色對應的資源id  
  17.     final int[] colors = new int[]{  
  18.             R.color.color1,  
  19.             R.color.color2,  
  20.             R.color.color3,  
  21.             R.color.color4,  
  22.             R.color.color5,  
  23.             R.color.color6  
  24.     };  
  25.     //View組件對應的資源id  
  26.     final int[] names = new int[]{  
  27.             R.id.tv_1,  
  28.             R.id.tv_2,  
  29.             R.id.tv_3,  
  30.             R.id.tv_4,  
  31.             R.id.tv_5,  
  32.             R.id.tv_6  
  33.     };  
  34.       
  35.     //組件數組  
  36.     TextView[] views = new TextView[names.length];  
  37.       
  38.     //定義這個Handler, 爲了在定時器中固定調用handleMessage方法  
  39.     Handler handler = new Handler(){  
  40.         public void handleMessage(Message msg) {  
  41.             if(msg.what == 0x123){  
  42.                 for(int i = 0; i < names.length; i ++){  
  43.                     views[i].setBackgroundResource(colors[(i + currentColor) % names.length]);  
  44.                 }  
  45.                 currentColor ++;  
  46.             }  
  47.         };  
  48.     };  
  49.       
  50.     @Override  
  51.     public void onCreate(Bundle savedInstanceState) {  
  52.         super.onCreate(savedInstanceState);  
  53.         setContentView(R.layout.frame);  
  54.         //初始化組件數組  
  55.         for(int i = 0; i < names.length; i ++){  
  56.             views[i] = (TextView) findViewById(names[i]);  
  57.         }  
  58.         //每隔0.2秒更換一次顏色  
  59.         new Timer().schedule(new TimerTask() {  
  60.             @Override  
  61.             public void run() {  
  62.                 handler.sendEmptyMessage(0x123);  
  63.             }  
  64.         }, 0200);  
  65.     }  
  66. }  


3. 三個水平方向的按鈕分別左對齊,居中對齊,右對齊




要點 : 給FrameLayout中的三個按鈕分別設置 不同的layout_gravity, left , center_horizontal, right, 就能設置成上圖的樣式;


代碼 : 


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.       
  6.     <Button   
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="按鈕1"  
  10.         android:layout_gravity="left"/>  
  11.       
  12.     <Button   
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="按鈕2"  
  16.         android:layout_gravity="center_horizontal"/>  
  17.       
  18.     <Button   
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="按鈕3"  
  22.         android:layout_gravity="right"/>  
  23.   
  24. </FrameLayout>  



四. 表格佈局TableLayout


1. 表格佈局的一些概念


繼承關係 : 表格佈局繼承了LinearLayout, 其本質是線性佈局管理器; 

控制組件 : 表格佈局採用 行, 列 形式管理子組件, 但是並不需要聲明有多少 行列, 只需要添加TableRow 和 組件 就可以控制表格的行數和列數;

增加行的方法 : 

a. TableRow增加行列 : 向TableLayout中添加一個TableRow, 一個TableRow就是一個表格行, 同時TableRow也是容器, 可以向其中添加子元素, 每添加一個組件, 就增加了一列;

b. 組件增加行 : 如果直接向TableLayout中添加組件, 就相當於直接添加了一行;


列寬 : TableLayout中, 列的寬度由該列最寬的單元格決定, 整個表格的寬度默認充滿父容器本身;


2. 單元格行爲方式 


(1) 行爲方式概念

 

a. 收縮 : Shrinkable, 如果某列被設爲Shrinkable, 那麼該列所有單元格寬度可以被收縮, 保證表格能適應父容器的寬度;

b. 拉伸 : Stretchable, 如果某列被設爲Stretchable, 那麼該列所有單元格的寬度可以被拉伸, 保證表格能完全填滿表格剩餘空間;

d. 隱藏 : Collapsed, 如果某列被設置成Collapsed, 那麼該列所有單元格會被隱藏;


(2) 行爲方式屬性


a. 隱藏

xml屬性 : android:collapsedColumns;

設置方法 : setColumnCollapsed(int, boolean);

作用 : 設置需要被隱藏的列的序號, 在xml文件中, 如果隱藏多列, 多列序號間用逗號隔開;


b. 拉伸

xml屬性 : android:stretchColumns;

設置方法 : setStretchAllColumns(boolean);

作用 : 設置允許被拉伸的列的序列號, xml文件中多個序列號之間用逗號隔開;


c. 收縮

xml屬性 : android:shrinkableColumns;

設置方法 : setShrinkableAllColumns(boolean);

作用 : 設置允許被收縮的列的序號, xml文件中, 多個序號之間可以用逗號隔開;


3. 表格佈局實例




實現要點 : 

獨自一行按鈕 : 向TableLayout中添加按鈕, 這個按鈕就會獨自佔據一行;

收縮按鈕 : 在TableLayout標籤中, 設置android:stretchable屬性標籤, 屬性值是要收縮的列, 注意, 列標從0開始;

拉伸按鈕 : 在TableLayout標籤中, 設置android:shrinkable屬性標籤, 屬性值是要拉伸的列, 注意, 列表從0開始;


代碼 : 


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.     <!-- LinearLayout默認是水平的, 這裏設置其方向爲垂直 -->  
  7.       
  8.     <!-- 表格佈局, 第2列允許收縮, 第3列允許拉伸, 注意這裏行列的計數都是從0開始的 -->  
  9.     <TableLayout   
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:shrinkColumns="1"  
  13.         android:stretchColumns="2">  
  14.           
  15.         <!-- 向TableLayout中直接添加組件, 獨佔一行 -->  
  16.         <Button   
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="獨自一行的按鈕"/>  
  20.           
  21.         <TableRow>  
  22.             <Button   
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:text="普通的按鈕"/>  
  26.             <Button   
  27.                 android:layout_width="wrap_content"  
  28.                 android:layout_height="wrap_content"  
  29.                 android:text="收縮的按鈕"/>  
  30.             <Button   
  31.                 android:layout_width="wrap_content"  
  32.                 android:layout_height="wrap_content"  
  33.                 android:text="拉伸的按鈕"/>  
  34.         </TableRow>  
  35.           
  36.     </TableLayout>  
  37.       
  38.     <!-- 第二個按鈕會隱藏掉 -->  
  39.     <TableLayout   
  40.         android:layout_width="fill_parent"  
  41.         android:layout_height="wrap_content"  
  42.         android:collapseColumns="1">  
  43.           
  44.         <Button   
  45.             android:layout_width="fill_parent"  
  46.             android:layout_height="wrap_content"  
  47.             android:text="獨自一行的按鈕"/>  
  48.           
  49.         <TableRow >  
  50.             <Button   
  51.                 android:layout_width="wrap_content"  
  52.                 android:layout_height="wrap_content"  
  53.                 android:text="普通按鈕1"/>  
  54.             <Button   
  55.                 android:layout_width="wrap_content"  
  56.                 android:layout_height="wrap_content"  
  57.                 android:text="普通按鈕2"/>  
  58.             <Button   
  59.                 android:layout_width="wrap_content"  
  60.                 android:layout_height="wrap_content"  
  61.                 android:text="普通按鈕3"/>  
  62.         </TableRow>  
  63.           
  64.     </TableLayout>  
  65.   
  66.     <!-- 指定第二列和第三列可以被拉伸 -->  
  67.     <TableLayout   
  68.         android:layout_height="wrap_content"  
  69.         android:layout_width="fill_parent"  
  70.         android:stretchColumns="1,2">  
  71.           
  72.         <Button   
  73.             android:layout_width="fill_parent"  
  74.             android:layout_height="wrap_content"  
  75.             android:text="獨自佔一行的按鈕"/>  
  76.           
  77.         <TableRow >  
  78.               
  79.             <Button   
  80.                 android:layout_width="wrap_content"  
  81.                 android:layout_height="wrap_content"  
  82.                 android:text="普通按鈕1"/>  
  83.             <Button   
  84.                 android:layout_width="wrap_content"  
  85.                 android:layout_height="wrap_content"  
  86.                 android:text="拉伸的按鈕"/>  
  87.             <Button   
  88.                 android:layout_width="wrap_content"  
  89.                 android:layout_height="wrap_content"  
  90.                 android:text="拉伸的按鈕"/>  
  91.               
  92.         </TableRow>  
  93.           
  94.         <TableRow >  
  95.               
  96.             <Button   
  97.                 android:layout_width="wrap_content"  
  98.                 android:layout_height="wrap_content"  
  99.                 android:text="普通的按鈕"/>  
  100.             <Button   
  101.                 android:layout_width="wrap_content"  
  102.                 android:layout_height="wrap_content"  
  103.                 android:text="拉伸的按鈕"/>  
  104.               
  105.         </TableRow>  
  106.           
  107.     </TableLayout>  
  108.       
  109.       
  110. </LinearLayout>  


五. 網格佈局


1. 網格佈局介紹


網格佈局時Android4.0版本纔有的, 在低版本使用該佈局需要導入對應支撐庫;

GridLayout將整個容器劃分成rows * columns個網格, 每個網格可以放置一個組件. 還可以設置一個組件橫跨多少列, 多少行. 不存在一個網格放多個組件情況;


2. 網格佈局常用屬性


(1) 設置對齊模式


xml屬性 : android:alignmentMode;

設置方法 : setAlignmentMode(int);

作用 : 設置網格佈局管理器的對齊模式


(2) 設置列數


xml屬性 : android:columnCount;

設置方法 : setColumnCount(int);

作用 : 設置該網格佈局的列數;


(3) 設置是否保留列序列號


xml屬性 : android:columnOrderPreserved;

設置方法 : setColumnOrderPreserved(boolean);

作用 : 設置網格容器是否保留列序列號;


(4) 設置行數


xml屬性 : android:rowCount;

設置方法 : setRowCount(int);

作用 : 設置該網格的行數;


(5) 設置是否保留行序列號


xml屬性 : android:rowOrderPreserved;

設置方法 : setRowOrderPreserved(int);

作用 : 設置該網格容器是否保留行序列號;


(6) 頁邊距


xml屬性 : android:useDefaultMargins;

設置方法 : setUseDefaultMargins(boolean);

作用 : 設置該佈局是否使用默認的頁邊距;


3. GridLayout的LayoutParams屬性


(1) 設置位置列


xml屬性 : android:layout_column;

作用 : 設置子組件在GridLayout的哪一列;


(2) 橫向跨列


xml屬性 : android:layout_columnSpan;

作用 : 設置該子組件在GridLayout中橫向跨幾列;


(3) 佔據空間方式


xml屬性 : android:layout_gravity;

設置方法 : setGravity(int);

作用 : 設置該組件採用何種方式佔據該網格的空間;


(4) 設置行位置


xml屬性 : android:layout_row;

作用 : 設置該子組件在GridLayout的第幾行;


(5) 設置橫跨行數


xml屬性 : android:layout_rowSpan;

作用 : 設置該子組件在GridLayout縱向橫跨幾行;


4. 實現一個計算機界面






(1) 佈局代碼


設置行列 : 設置GridLayout的android:rowCount爲6, 設置android:columnCount爲4, 這個網格爲 6行 * 4列 的;

設置橫跨四列 : 設置TextView和按鈕橫跨四列 android:layout_columnSpan 爲4, 列的合併 就是佔了一行;

textView的一些設置 : 

設置textView中的文本與邊框有5像素間隔 : android:padding = "5px";


代碼 : 


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"   
  5.     android:rowCount="6"  
  6.     android:columnCount="4"  
  7.     android:id="@+id/root">  
  8.       
  9.     <!--   
  10.         定義一個  6行 * 4列 GridLayout, 在裏面定義兩個組件  
  11.         兩個組件都橫跨4列, 單獨佔一行  
  12.      -->  
  13.     <TextView   
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_columnSpan="4"  
  17.         android:textSize="50sp"  
  18.         android:layout_marginLeft="4px"  
  19.         android:layout_marginRight="4px"  
  20.         android:padding="5px"  
  21.         android:layout_gravity="right"  
  22.         android:background="#eee"  
  23.         android:textColor="#000"  
  24.         android:text="0"/>  
  25.       
  26.     <Button   
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_columnSpan="4"  
  30.         android:text="清除"/>  
  31.   
  32. </GridLayout>  


(2) Activity代碼


將組建設置給GridLayout網格流程 : 

指定組件所在行 : GridLayout.Spec rowSpec = GridLayout.spec(int); 

指定組件所在列 : GridLayout.Spec columnSpec = GridLayout.spec(int);

創建LayoutParams對象 : GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, columnSpec);

指定組件佔滿容器 : params.setGravity(Gravity.FILL);

將組件添加到佈局中 : gridLayout.addView(view, params);


代碼 : 


[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package com.example.caculator;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.widget.Button;  
  7. import android.widget.GridLayout;  
  8. import android.widget.GridLayout.LayoutParams;  
  9. import android.widget.GridLayout.Spec;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.     private GridLayout gridLayout;  
  14.     //需要放到按鈕上的字符串  
  15.     String chars[] = new String[]{  
  16.         "7""8""9""/",  
  17.         "4""5""6""*",  
  18.         "1""2""3""-",  
  19.         ".""0""=""+"  
  20.     };  
  21.       
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.           
  27.         gridLayout = (GridLayout) findViewById(R.id.root);  
  28.         for(int i = 0; i < chars.length; i ++){  
  29.             Button button = new Button(this);  
  30.             button.setText(chars[i]);  
  31.             button.setTextSize(40);  
  32.             //指定組件所在行  
  33.             Spec rowSpec = GridLayout.spec(i / 4 + 2);  
  34.             //指定組件所在列  
  35.             Spec columnSpec = GridLayout.spec(i % 4);  
  36.             //生成LayoutParams對象  
  37.             LayoutParams layoutParams = new LayoutParams(rowSpec, columnSpec);  
  38.             //指定組件充滿網格  
  39.             layoutParams.setGravity(Gravity.FILL);  
  40.             //將組件設置給GridLayout  
  41.             gridLayout.addView(button, layoutParams);  
  42.         }  
  43.     }  
  44. }  
發佈了4 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章