在移動端應用程序開發中,經常會使用到表格佈局,iOS和Android開發框架中都提供了獨立的表格視圖控件供開發者使用,例如iOS中的UITableView、UICollectionView,Android中的ListView、GridView等。除了獨立的視圖控件外,Android中還提供了一個佈局容器類TableLayout,使用其也能夠進行方便的表格佈局。java
前邊博客有介紹過關於LinearLayout線性佈局的相關內容,LinearLayout只能進行水平或者垂直方向上的排列布局,使用LinearLayout的佈局嵌套,實際上也能夠實現表格佈局的樣式。實際上,TableLayout就是採用這樣的原理,TableLayout繼承於LinearLayout,其中每一個視圖元素做爲一行,同時Android中還提供了一個TableRow類,這個類一樣繼承自LinearLayout,其中每一個視圖元素做爲當前行中的一列,結合使用TableLayout與TableRow,就實現了行列的表格佈局。框架
TableRow能夠簡單理解爲TableLayout佈局中的一行,固然,TableLayout中也能夠直接添加任意的View視圖,可是默認添加的View視圖將獨佔一行。TableRow中能夠添加其餘視圖,每一個視圖被做爲一列處理,經過TableRow的內部類LayoutParams來設置TableRow內部視圖的佈局方式,其中主要能夠經過設置寬高或者設置權重來定製每列視圖元素的尺寸,例如:ide
TableLayout tableLayout = new TableLayout(this); //建立行 第一行用單個元素 TextView textView = new TextView(this); textView.setText("1000"); textView.setTextSize(20); textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END); tableLayout.addView(textView); //第二行使用TableRow TableRow tableRow1 = new TableRow(this); //設置本行中每一列的權重和 tableRow1.setWeightSum(10); Button button11 = new Button(this); button11.setText("AC"); //設置固定寬高 TableRow.LayoutParams layoutParams1 = new TableRow.LayoutParams(300,200); button11.setLayoutParams(layoutParams1); tableRow1.addView(button11); Button button12 = new Button(this); //經過權重設置列的寬度 佔正常列寬的一半 TableRow.LayoutParams layoutParams2 = new TableRow.LayoutParams(0,200,5); button12.setLayoutParams(layoutParams2); button12.setText("+/-"); tableRow1.addView(button12); Button button13 = new Button(this); button13.setText("%"); tableRow1.addView(button13); Button button14 = new Button(this); button14.setText("÷"); tableRow1.addView(button14); tableLayout.addView(tableRow1);
上面代碼向TableRow中添加了4個視圖,默認狀況下會生成四列,setWeightSum()方法用於設置每列的權重和,須要注意,它做用的對象是每一列元素,而不是整行。上面的代碼效果以下:佈局
默認的列寬是評分整個行寬,能夠經過指定寬度或者權重來修改特定列的列寬。this
還有一點須要注意,若是一個TableLayout佈局中多個TableRow,則表格的列數會以最多列的一行爲準,例如在添加一行TableRow,而其中只有一列,則其依然會預留4列的位置,示例以下:spa
TableRow tableRow2 = new TableRow(this); Button button = new Button(this); button.setText("跳過"); tableRow2.addView(button); tableLayout.addView(tableRow2);
效果以下:code
也能夠設置跳過某列進行佈局,或者進行列的合併,示例以下:對象
TableRow tableRow2 = new TableRow(this); Button button = new Button(this); button.setText("跳過"); TableRow.LayoutParams layoutParams21 = new TableRow.LayoutParams(); //從第2列開始 layoutParams21.column = 1; //合併3列 layoutParams21.span = 3; button.setLayoutParams(layoutParams21); tableRow2.addView(button); tableLayout.addView(tableRow2);
在向TableLayout容器中添加或者移除視圖的時候,開發者能夠對其進行監聽,示例以下:繼承
TableLayout tableLayout = new TableLayout(this); tableLayout.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() { @Override public void onChildViewAdded(View parent, View child) { Toast.makeText(getBaseContext(),"add",Toast.LENGTH_SHORT).show(); } @Override public void onChildViewRemoved(View parent, View child) { Toast.makeText(getBaseContext(),"remove",Toast.LENGTH_SHORT).show(); } });
開發者還能夠對錶格中視圖元素的一些尺寸自適應作一些設置,其中還有一些經常使用的方法列舉以下:開發
//獲取表格中全部列是不是可收縮的 public boolean isShrinkAllColumns() //設置表格中的全部列是否可收縮 public void setShrinkAllColumns() //獲取表格中的全部列是否可拉伸 public boolean isStretchAllColumns() //設置表格中的全部列是否可拉伸 public void setStretchAllColumns() //設置某一列是否可拉伸 public void setColumnStretchable(int columnIndex, boolean isStretchable) //獲取某一列是否可拉伸 public boolean isColumnStretchable(int columnIndex) //設置某一列是否可收縮 public void setColumnShrinkable(int columnIndex, boolean isShrinkable) //獲取某一列是否可收縮 public boolean isColumnShrinkable(int columnIndex)
所謂可收縮的列,是指若是此列的內容寬度超出必定寬度,爲了使後面的列內容展現出來,此列寬度會自動收縮,高度會增長,以下圖所示:
至於可拉伸的列,是指若是此行內容內有充滿整行,此列會進行拉伸自動充滿。
下面這些方法與表格中列的隱藏有關:
//設置某列是否隱藏 public void setColumnCollapsed(int columnIndex, boolean isCollapsed) //獲取某列是否被隱藏 public boolean isColumnCollapsed(int columnIndex)
須要注意,在TableLayout中也定義了一個LayoutParams的內部類,其用於設置其中每一行視圖元素的佈局,可是開發者只能設置此佈局類對應的高度參數,寬度將強制設置爲MATCH_PARENT。
專一技術,熱愛生活,交流技術,也作朋友。
——琿少 QQ羣:435043639