【079】用代碼來建立 Android 控制項

來源:互聯網
上載者:User

  一般來說我們在建立控制項的時候都是在 XML 檔案中完成的, 實施起來還是蠻方便的, 而且修改起來也可以很快的看見效果, 但是有一個很大的劣勢就是沒辦法動態建立控制項, 舉個例子, 例如我從資料庫中取出資料想要存放在 tableLayout 中, 這時由於不知道行數和列數, 因此就沒辦法在 XML 中建立了, 另外有的時候需要同時建立一些樣式和功能相近的控制項, 要是在 XML 中一直複製, 還是挺煩的, 因為可以在代碼中用迴圈語句實現建立, 這樣建立方便, 修改也更加方便, 下面就介紹如何通過代碼來動態地建立 Android 控制項.

  我們在用 XML 建立控制項的時候, 一般都是先設定布局, 然後設定布局的長寬, 在設定控制項的時候, 也是要設定控制項的長寬, 在 XML 中用 android:layout_width 表示布局的寬度, 下面通過代碼來實現. 對於其他的一些設定可以在 【063】 Android API < I > - 控制項和布局 中查看.

目錄:

  1. ViewGroup.LayoutParams方
  2. LinearLayout.LayoutParams方
  3. 舉例說明

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第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();

---------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.