博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【079】用代码来创建 Android 控件
阅读量:7286 次
发布时间:2019-06-30

本文共 3330 字,大约阅读时间需要 11 分钟。

  一般来说我们在创建控件的时候都是在 XML 文件中完成的, 实施起来还是蛮方便的, 而且修改起来也可以很快的看见效果, 但是有一个很大的劣势就是没办法动态的创建控件, 举个例子, 例如我从数据库中取出数据想要存放在 tableLayout 中, 这时由于不知道行数和列数, 因此就没办法在 XML 中创建了, 另外有的时候需要同时创建一些样式和功能相近的控件, 要是在 XML 中一直复制, 还是挺烦的, 因为可以在代码中用循环语句实现创建, 这样创建方便, 修改也更加方便, 下面就介绍如何通过代码来动态地创建 Android 控件.

  我们在用 XML 创建控件的时候, 一般都是先设置布局, 然后设置布局的长宽, 在设置控件的时候, 也是要设置控件的长宽, 在 XML 中用 android:layout_width 表示布局的宽度, 下面通过代码来实现. 对于其他的一些设置可以在  中查看.

目录:

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

            ╔════════╗

╠════╣        ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● 

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, 用像素值.

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

            ╔════════╗

╠════╣        ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● 

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();

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

 

 

 

 

 

 

转载于:https://www.cnblogs.com/alex-bn-lee/archive/2012/08/26/2657172.html

你可能感兴趣的文章
ubuntu 64 装db2 v9.7 server
查看>>
顶级操作系统会议——2009年SOSP会议概况介绍
查看>>
display:table-cell实现两栏自适应布局
查看>>
mysql 读写分离mysql-proxy 代理
查看>>
httpd+tomcat(3) -- mod_jk
查看>>
MySQL:卸载、安装MySQL8.***
查看>>
CentOS 7安装Docker及常用命令
查看>>
VMware Workstation 7.0中文版下载
查看>>
Don’t forget about column projection
查看>>
linux系统修复及忘记密码的处理方法
查看>>
CAS和ABA问题
查看>>
js创建对象的几种常用方式
查看>>
SQL Server AlwaysOn可用性及故障转移
查看>>
Spring Cloud 注册中心高可用搭建
查看>>
js 简单版本号比较
查看>>
Linux用户配置sudo权限(visudo)
查看>>
rocketmq 事物消息压测
查看>>
eclipse debug 多线程
查看>>
ubuntu System Settings 里面的内容显示不正常
查看>>
Udp传输入门
查看>>