一般來說我們在建立控制項的時候都是在 XML 檔案中完成的, 實施起來還是蠻方便的, 而且修改起來也可以很快的看見效果, 但是有一個很大的劣勢就是沒辦法動態建立控制項, 舉個例子, 例如我從資料庫中取出資料想要存放在 tableLayout 中, 這時由於不知道行數和列數, 因此就沒辦法在 XML 中建立了, 另外有的時候需要同時建立一些樣式和功能相近的控制項, 要是在 XML 中一直複製, 還是挺煩的, 因為可以在代碼中用迴圈語句實現建立, 這樣建立方便, 修改也更加方便, 下面就介紹如何通過代碼來動態地建立 Android 控制項.
我們在用 XML 建立控制項的時候, 一般都是先設定布局, 然後設定布局的長寬, 在設定控制項的時候, 也是要設定控制項的長寬, 在 XML 中用 android:layout_width 表示布局的寬度, 下面通過代碼來實現. 對於其他的一些設定可以在 【063】 Android API < I > - 控制項和布局 中查看.
目錄:
- ViewGroup.LayoutParams方
- LinearLayout.LayoutParams方
- 舉例說明
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A1個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● LinearLayout.LayoutParams:
1. 用來建立 LinearLayout 的布局. 在 LinearLayout.setLayoutParams(ViewGroup.LayoutParams params) 方法中賦值和調用.
java.lang.Object
↳android.view.ViewGroup.LayoutParams
↳android.view.ViewGroup.MarginLayoutParams
↳android.widget.LinearLayout.LayoutParams
2. Constructors:
- LinearLayout.LayoutParams(int width, int height):可以用常量, 也可以用數字.
- LinearLayout.LayoutParams(int width, int height, float weight):
3. Constants:
- FILL_PARENT:傳回值:int.
- MATCH_PARENT:傳回值:int.
- WRAP_CONTENT:傳回值:int.
4. Fields:
- gravity:對齊樣式.(int)
Gravity.CENTER:水平左右置中.
Gravity.CENTER_HORIZONTAL:水平置中.
- weight:權重.(float)
- bottomMargin:
- leftMargin:
- rightMargin:
- topMargin:
5. Methods:
- setMargins(int left, int top, int right, int bottom):設定 margins, 用像素值.
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A0個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● ViewGroup.LayoutParams:
1. 為其他 LayoutParams 的基類, 所以此類的執行個體可以用於任何控制項的布局, 其他的 LayoutParams 只是在此基礎之上, 加入了一些符合自己的東東.
2. Constructors:
- LinearLayout.LayoutParams(int width, int height):可以用常量, 也可以用數字.
3. Constants:
- FILL_PARENT:傳回值:int.
- MATCH_PARENT:傳回值:int.
- WRAP_CONTENT:傳回值:int.
4. Fields:
- height:高度.(int)
- width:寬度.(int)
舉例說明
tableLayout.removeAllViews(); //清除內部所有控制項dbAdapter.open();Cursor cursor = dbAdapter.getAllStudents();int numberOfRow = cursor.getCount(); //一共的行數 int numberOfColumn = 5; //一共的列數for (int row = 0; row < numberOfRow; row ++){ //對行迴圈 TableRow tableRow = new TableRow(Database.this); //建立 tableRow 對象 tableRow.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); //添加布局 for (int column =0; column < numberOfColumn; column++){ //在每一行內, 對列迴圈 TextView textView = new TextView(Database.this); //建立 textView 對象 textView.setText(" " + cursor.getString(column) + " "); //賦值顯示文本 if(column == 0) {textView.setBackgroundColor(Color.RED);textView.setTextColor(Color.CYAN);textView.setGravity(Gravity.CENTER);} if(column == 1) {textView.setBackgroundColor(Color.YELLOW);textView.setTextColor(Color.BLACK);} if(column == 2) {textView.setBackgroundColor(Color.GREEN);textView.setTextColor(Color.BLACK);} if(column == 3) {textView.setBackgroundColor(Color.rgb(100, 0, 200));textView.setTextColor(Color.BLACK);} if(column == 4) {textView.setBackgroundColor(Color.CYAN);textView.setTextColor(Color.BLACK);} tableRow.addView(textView); } tableLayout.addView(tableRow); cursor.moveToNext();
---------------------------------------------------------------------------------------------------------