【079】用代码来创建 Android 控件

  一般来说我们在创建控件的时候都是在 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();

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

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值