alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【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();
复制代码

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

 

 

 

 

 

 

posted on   McDelfino  阅读(1239)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示