佈局管理器都是以ViewGroup爲基類派生出來的; 使用佈局管理器能夠適配不一樣手機屏幕的分辨率,尺寸大小;html
佈局管理器之間的繼承關係 : java
在上面的UML圖中能夠看出, 絕對佈局 幀佈局 網格佈局 相對佈局 線性佈局是直接繼承ViewGroup,表格佈局是繼承的LinearLayout;android
做用 : 線性佈局會將容器中的組件一個一個排列起來, LinearLayout能夠控制組件 橫向 或者 縱向 排列, 經過android:orientation屬性控制;數組
不換行屬性 : 線性佈局中的組件不會自動換行, 若是組件一個一個排列到盡頭以後, 剩下的組件就不會顯示出來;app
xml屬性 : android:baselineAligned; ide
設置方法 : setBaselineAligned(boolean b); 佈局
做用 : 若是該屬性爲false, 就會阻止該佈局管理器與其子元素的基線對齊;測試
xml屬性 : android:divider; this
設置方法 : setDividerDrawable(Drawable); spa
做用 : 設置垂直佈局時兩個按鈕之間的分隔條;
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 左側垂直居中;
xml屬性 : android:measureWithLargestChild;
設置方法 : setMeasureWithLargestChildEnable(boolean b);
做用 : 該屬性爲true的時候, 全部帶權重的子元素都會具備最大子元素的最小尺寸;
xml屬性 : android:orientation;
設置方法 : setOrientation(int i);
做用 : 設置佈局管理器內組件排列方式, 設置爲horizontal(水平),vertical(垂直), 默認爲垂直排列;
LinearLayout的子元素, 即LinearLayout中的組件, 都受到LinearLayout.LayoutParams控制, 所以LinearLayout包含的子元素能夠執行下面的屬性.
xml屬性 : android:layout_gravity;
做用 : 指定該元素在LinearLayout(父容器)的對齊方式;
xml屬性 : android:layout_gravity;
做用 : 指定該元素在LinearLayout(父容器)中所佔的權重;
控制自己元素屬性與子元素屬性 : 帶layout的屬性是設置自己, 例如 android:layout_gravity設置的是自己的對其方式; 不帶layout的屬性是設置其所包含的子元素, 例如android:gravity 設置的是該容器子組件的對齊方式;
全部的佈局管理器都提供了相應的LayoutParams內部類, 這些內部類用於控制佈局中子元素的佈局, 如 對齊方式 layout_gravity, 所佔權重 layout_weight, 這些屬性用於設置本元素在父容器中的對齊方式;
android:gravity做用是指定指定本元素包含的子元素的對齊方式, 只有容器才支持這個屬性;
調用View.getHeight() 和 View.getWidth()方法 是獲取不到組件的寬度和高度的, 這兩個方法返回的是0; Android的運行機制決定了沒法在組件外部使用getHeight()和getWidth()方法獲取寬度和高度, 在自定義的類中能夠在View的類中經過調用這兩個方法獲取該View子類組件的寬和高;
使用View.getMeasuredWidth() 和 View.getMeasuredHeight()方法能夠獲取組件的寬和高, 在調用這個方法以前, 必須先調用View.measure()方法, 才能夠, 不然也獲取不到組件的寬高;
注意 : 若是組件寬度或高度設置爲fill_parent, 使用getMeasuredHeight()等方法獲取寬度和高度的時候, 而且組件中含有子元素時, 所獲取的實際值是這些組件所佔的最小寬度和最小高度.
示例:
View view = getLayoutInflater().inflate(R.layout.main, null); LinearLayout layout = (LinearLayout) view.findViewById(R.id.linearlayout); //調用測量方法, 調用了該方法以後才能經過getMeasuredHeight()等方法獲取寬高 layout.measure(0, 0); //獲取寬度 int width = layout.getMeasuredWidth(); //獲取高度 int height = layout.getMeasuredHeight();
調用View.getLayoutParams().width 和 View.getLayoutParams().height 獲取寬高, 若是寬高被設定爲 fill_parent, match_parent, warp_content 時, 這兩個兩邊直接回返回 FILL_PARENT, MATCH_PARENT, WARP_CONTENT常量值;
若是佈局是vertical, 那麼設置一個ImageView寬度fill_parent, 高度2dp, 設置一個背景色;
若是佈局時horizontal, 那麼設置一個ImageView寬度2dp, 高度fill_parent, 設置一個背景色;
<ImageView android:layout_width="fill_parent" android:layout_height="2dp" android:background="#F00"/>
設置LinearLayout標籤的 android:showDividers 屬性, 該屬性有四個值 :
none : 不顯示分隔線;
beginning : 在LinearLayout開始處顯示分隔線;
middle : 在LinearLayout中每兩個組件之間顯示分隔線;
end : 在LinearLayout結尾處顯示分隔線;
設置android:divider屬性, 這個屬性的值是一個Drawable的id;
設置顯示分隔線樣式 : linearLayout.setShowDividers(), 設置android:showDividers屬性;
設置分隔線圖片 : linearLayout.setDividerDrawable(), 設置android:divider屬性;
要點 :
左邊的LinearLayout的android:gravity 屬性爲 bottom|center_horizontal;
右邊的LinearLayout的android:gravity 屬性爲 right|center_vertical;
代碼 :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="bottom|center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測試按鈕2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測試按鈕4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕5"/> </LinearLayout>
左邊的圖的屬性爲 bottom|center_horizontal , 右邊的android:gravity的屬性值爲 right|center_vertical;
三個水平方向的按鈕, 分別左對齊, 居中對齊, 右對齊 :
要點 :
最頂層的LinearLayout的orientation是horizontal水平的;
第二層的LinearLayout的orientation是vertical垂直的, 而且寬度是fill_parent , 依靠權重分配寬度;
按鈕的android:layout_gravity屬性根據需求 left, center, right, 默認爲left;
代碼 :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical" android:background="#f00"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical" android:background="#0f0"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" android:layout_gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical" android:background="#00f"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" android:layout_gravity="right"/> </LinearLayout> </LinearLayout>
相對佈局容器中, 子組件的位置老是相對兄弟組件, 父容器來決定的;
xml屬性 : android:gravity;
設置方法 : setGravity(int);
做用 : 設置佈局容器內子元素的對齊方式;
xml屬性 : android:ignoreGravity;
設置方法 : setIgnoreGravity(int);
做用 : 設置該組件不受gravity屬性影響, 由於gravity屬性影響容器內全部的組件的對齊方式, 設置了以後, 該組件就能夠例外;
這些屬性都是相對父容器的, 肯定是否在父容器中居中(水平, 垂直), 是否位於父容器的 上下左右 端;
是否水平居中 : android:layout_centerHorizontal;
是否垂直居中 : android:layout_centerVertical;
是否位於中央 : android:layout_centerInParent;
是否底端對齊 : android:layout_alignParentBottom;
是否頂端對齊 : android:layout_alignParentTop;
是否左邊對齊 : android:layout_alignParentLeft;
是否右邊對齊 : android:layout_alignParentRight;
位於所給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;
五個按鈕排成梅花形狀, 梅花處於正中心, 效果圖以下 :
兩個按鈕, 若是隻有 android:layout_above="@+id/bt1" 會是這種狀況 :
加上 android:layout_alignLeft="@+id/bt1"就會成爲這種狀況 :
要點 :
注意每一個組件的屬性, 先要肯定方位, 在進行對齊, 組件左邊界對齊, 組件上邊界對齊;
代碼 :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" android:layout_centerInParent="true"/> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" android:layout_above="@+id/bt1" android:layout_alignLeft="@+id/bt1"/> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" android:layout_centerInParent="true" android:layout_below="@+id/bt1" android:layout_alignLeft="@+id/bt1"/> <Button android:id="@+id/bt4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕4" android:layout_centerInParent="true" android:layout_toLeftOf="@+id/bt1" android:layout_alignTop="@+id/bt1"/> <Button android:id="@+id/bt5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕5" android:layout_centerInParent="true" android:layout_toRightOf="@+id/bt1" android:layout_alignTop="@+id/bt1"/> </RelativeLayout>
要先建立一個整型數組, 數組大小2位; 這個數組傳入getLocationOnScreen()方法;
能夠調用View.getLocationOnScreen()方法, 返回的是一個數組 int[2], int[0] 是橫座標, int[1] 是縱座標;
//獲取組件 Button b = (Button) this.findViewById(R.id.Button01); //建立數組, 將該數組傳入getLocationOnScreen()方法 int locations[] = new int[2]; //獲取位置信息 b.getLocationOnScreen(locations); //獲取寬度 int width = locations[0]; //獲取高度 int height = locations[1];
屬性設置方法少 : 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屬性 :
//裝載佈局文件 RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.relative, null); //裝載要動態添加的佈局文件 Button button = (Button) relativeLayout.findViewById(R.id.bt1); //建立一個LayoutParams對象 LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); //設置android:layout_toRightOf屬性 layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.bt2); //設置android:layout_below layoutParams.addRule(RelativeLayout.BELOW, R.id.bt2); //更新Button按鈕的屬性 button.setLayoutParams(layoutParams); //向佈局中動態添加按鈕 relativeLayout.addView(button);
幀佈局容器爲每一個組件建立一個空白區域, 一個區域成爲一幀, 這些幀會根據gravity屬性自動對齊;
繪製一個霓虹燈效果的層疊佈局, 以下圖 :
要點 :
後擋前 : 後面的View組件會遮擋前面的View組件, 越在前面, 被遮擋的機率越大;
界面居中 : 將全部的TextView組件的對齊方式 android:layout_gravity 設置爲center;
正方形 : 全部的TextView都設置android:height 和 android:width 屬性, 用來設置其寬高, 這裏設置成正方形, 寬高同樣, 後面的組件比前面的邊長依次少40;
顏色 : 每一個TextView的背景都設置成不同的;
代碼 :
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="320px" android:height="320px" android:background="#f00"/> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="280px" android:height="280px" android:background="#0f0"/> <TextView android:id="@+id/tv_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="240px" android:height="240px" android:background="#00f"/> <TextView android:id="@+id/tv_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="200px" android:height="200px" android:background="#ff0"/> <TextView android:id="@+id/tv_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="160px" android:height="160px" android:background="#f0f"/> <TextView android:id="@+id/tv_6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="120px" android:height="120px" android:background="#0ff"/> </FrameLayout>
建立顏色資源, 在跟節點<resources>下面建立<color>子節點, color屬性標籤 name 自定義, 子文本爲顏色代碼;
建立Handler對象, 實現handleMessage()方法, 在這個方法中循環設置 TextView對象的顏色變量, 使用color[(i + currentColor)%colors.length]每調用一次, 就將全部的TextView顏色依次調換一次;
在onCreate()方法中, 開啓一個定時器Timer, 每隔0.2s就調用一個handler中的方法, 這樣動態的霓虹燈代碼就顯示出來了.
顏色資源代碼 :
<?xml version="1.0" encoding="utf-8"?> <resources> <color name = "color1">#f00</color> <color name = "color2">#0f0</color> <color name = "color3">#00f</color> <color name = "color4">#ff0</color> <color name = "color5">#f0f</color> <color name = "color6">#0ff</color> </resources>
代碼 :
package com.example.framelayout; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; public class MainActivity extends Activity { //這個變量用來控制霓虹燈顏色變化 private int currentColor = 0; //顏色對應的資源id final int[] colors = new int[]{ R.color.color1, R.color.color2, R.color.color3, R.color.color4, R.color.color5, R.color.color6 }; //View組件對應的資源id final int[] names = new int[]{ R.id.tv_1, R.id.tv_2, R.id.tv_3, R.id.tv_4, R.id.tv_5, R.id.tv_6 }; //組件數組 TextView[] views = new TextView[names.length]; //定義這個Handler, 爲了在定時器中固定調用handleMessage方法 Handler handler = new Handler(){ public void handleMessage(Message msg) { if(msg.what == 0x123){ for(int i = 0; i < names.length; i ++){ views[i].setBackgroundResource(colors[(i + currentColor) % names.length]); } currentColor ++; } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.frame); //初始化組件數組 for(int i = 0; i < names.length; i ++){ views[i] = (TextView) findViewById(names[i]); } //每隔0.2秒更換一次顏色 new Timer().schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0x123); } }, 0, 200); } }
要點 : 給FrameLayout中的三個按鈕分別設置 不一樣的layout_gravity, left , center_horizontal, right, 就能設置成上圖的樣式;
代碼 :
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" android:layout_gravity="left"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" android:layout_gravity="center_horizontal"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" android:layout_gravity="right"/> </FrameLayout>
繼承關係 : 表格佈局繼承了LinearLayout, 其本質是線性佈局管理器;
控制組件 : 表格佈局採用 行, 列 形式管理子組件, 可是並不須要聲明有多少 行列, 只須要添加TableRow 和 組件 就能夠控制表格的行數和列數;
增長行的方法 :
a. TableRow增長行列 : 向TableLayout中添加一個TableRow, 一個TableRow就是一個表格行, 同時TableRow也是容器, 能夠向其中添加子元素, 每添加一個組件, 就增長了一列;
b. 組件增長行 : 若是直接向TableLayout中添加組件, 就至關於直接添加了一行;
列寬 : TableLayout中, 列的寬度由該列最寬的單元格決定, 整個表格的寬度默認充滿父容器自己;
a. 收縮 : Shrinkable, 若是某列被設爲Shrinkable, 那麼該列全部單元格寬度能夠被收縮, 保證表格能適應父容器的寬度;
b. 拉伸 : Stretchable, 若是某列被設爲Stretchable, 那麼該列全部單元格的寬度能夠被拉伸, 保證表格能徹底填滿表格剩餘空間;
d. 隱藏 : Collapsed, 若是某列被設置成Collapsed, 那麼該列全部單元格會被隱藏;
a. 隱藏
xml屬性 : android:collapsedColumns;
設置方法 : setColumnCollapsed(int, boolean);
做用 : 設置須要被隱藏的列的序號, 在xml文件中, 若是隱藏多列, 多列序號間用逗號隔開;
b. 拉伸
xml屬性 : android:stretchColumns;
設置方法 : setStretchAllColumns(boolean);
做用 : 設置容許被拉伸的列的序列號, xml文件中多個序列號之間用逗號隔開;
c. 收縮
xml屬性 : android:shrinkableColumns;
設置方法 : setShrinkableAllColumns(boolean);
做用 : 設置容許被收縮的列的序號, xml文件中, 多個序號之間能夠用逗號隔開;
實現要點 :
獨自一行按鈕 : 向TableLayout中添加按鈕, 這個按鈕就會獨自佔據一行;
收縮按鈕 : 在TableLayout標籤中, 設置android:stretchable屬性標籤, 屬性值是要收縮的列, 注意, 列標從0開始;
拉伸按鈕 : 在TableLayout標籤中, 設置android:shrinkable屬性標籤, 屬性值是要拉伸的列, 注意, 列表從0開始;
代碼 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- LinearLayout默認是水平的, 這裏設置其方向爲垂直 --> <!-- 表格佈局, 第2列容許收縮, 第3列容許拉伸, 注意這裏行列的計數都是從0開始的 --> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:shrinkColumns="1" android:stretchColumns="2"> <!-- 向TableLayout中直接添加組件, 獨佔一行 --> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="獨自一行的按鈕"/> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通的按鈕"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收縮的按鈕"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按鈕"/> </TableRow> </TableLayout> <!-- 第二個按鈕會隱藏掉 --> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:collapseColumns="1"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="獨自一行的按鈕"/> <TableRow > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕3"/> </TableRow> </TableLayout> <!-- 指定第二列和第三列能夠被拉伸 --> <TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:stretchColumns="1,2"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="獨自佔一行的按鈕"/> <TableRow > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按鈕"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按鈕"/> </TableRow> <TableRow > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通的按鈕"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按鈕"/> </TableRow> </TableLayout> </LinearLayout>
網格佈局時Android4.0版本纔有的, 在低版本使用該佈局須要導入對應支撐庫;
GridLayout將整個容器劃分紅rows * columns個網格, 每一個網格能夠放置一個組件. 還能夠設置一個組件橫跨多少列, 多少行. 不存在一個網格放多個組件狀況;
xml屬性 : android:alignmentMode;
設置方法 : setAlignmentMode(int);
做用 : 設置網格佈局管理器的對齊模式
xml屬性 : android:columnCount;
設置方法 : setColumnCount(int);
做用 : 設置該網格佈局的列數;
xml屬性 : android:columnOrderPreserved;
設置方法 : setColumnOrderPreserved(boolean);
做用 : 設置網格容器是否保留列序列號;
xml屬性 : android:rowCount;
設置方法 : setRowCount(int);
做用 : 設置該網格的行數;
xml屬性 : android:rowOrderPreserved;
設置方法 : setRowOrderPreserved(int);
做用 : 設置該網格容器是否保留行序列號;
xml屬性 : android:useDefaultMargins;
設置方法 : setUseDefaultMargins(boolean);
做用 : 設置該佈局是否使用默認的頁邊距;
xml屬性 : android:layout_column;
做用 : 設置子組件在GridLayout的哪一列;
xml屬性 : android:layout_columnSpan;
做用 : 設置該子組件在GridLayout中橫向跨幾列;
xml屬性 : android:layout_gravity;
設置方法 : setGravity(int);
做用 : 設置該組件採用何種方式佔據該網格的空間;
xml屬性 : android:layout_row;
做用 : 設置該子組件在GridLayout的第幾行;
xml屬性 : android:layout_rowSpan;
做用 : 設置該子組件在GridLayout縱向橫跨幾行;
設置行列 : 設置GridLayout的android:rowCount爲6, 設置android:columnCount爲4, 這個網格爲 6行 * 4列 的;
設置橫跨四列 : 設置TextView和按鈕橫跨四列 android:layout_columnSpan 爲4, 列的合併 就是佔了一行;
textView的一些設置 :
設置textView中的文本與邊框有5像素間隔 : android:padding = "5px";
代碼 :
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4" android:id="@+id/root"> <!-- 定義一個 6行 * 4列 GridLayout, 在裏面定義兩個組件 兩個組件都橫跨4列, 單獨佔一行 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:textSize="50sp" android:layout_marginLeft="4px" android:layout_marginRight="4px" android:padding="5px" android:layout_gravity="right" android:background="#eee" android:textColor="#000" android:text="0"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除"/> </GridLayout>
將組建設置給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);
代碼 :
package com.example.caculator; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.widget.Button; import android.widget.GridLayout; import android.widget.GridLayout.LayoutParams; import android.widget.GridLayout.Spec; public class MainActivity extends Activity { private GridLayout gridLayout; //須要放到按鈕上的字符串 String chars[] = new String[]{ "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", ".", "0", "=", "+" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridLayout = (GridLayout) findViewById(R.id.root); for(int i = 0; i < chars.length; i ++){ Button button = new Button(this); button.setText(chars[i]); button.setTextSize(40); //指定組件所在行 Spec rowSpec = GridLayout.spec(i / 4 + 2); //指定組件所在列 Spec columnSpec = GridLayout.spec(i % 4); //生成LayoutParams對象 LayoutParams layoutParams = new LayoutParams(rowSpec, columnSpec); //指定組件充滿網格 layoutParams.setGravity(Gravity.FILL); //將組件設置給GridLayout gridLayout.addView(button, layoutParams); } } }