千家信息网

Android如何使用代码动态生成界面

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍Android如何使用代码动态生成界面,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!我们最常用使用XML来编写Android应用程序的UI,这样的好处是方便快捷
千家信息网最后更新 2026年11月03日Android如何使用代码动态生成界面

这篇文章主要介绍Android如何使用代码动态生成界面,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

我们最常用使用XML来编写Android应用程序的UI,这样的好处是方便快捷可视化,而且维护和修改特别容易,但是它是静态的。如果我们要做的程序的界面是固定的,用XML固然是最好的选择,但是如果我们需要动态、灵活地控制UI,使用代码来动态生成UI无疑使最好的办法。

在XML中,我们使用的五大布局:LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、AbsoluteLayout(绝对布局)和FrameLayout(帧布局)在Android中也有对应的类来表示。

举个例子,我现在需要显示一个表格,表格的行数和列数及其内容都不确定,如果在XML中,这是不可能实现的。

先给大家看一下成品:(下面的代码只给大家展示如何实现,表格里面的内容忽略)

首先,新建一个不带任何控件的XML文件

          

在代码中新建一个TableLayout:

// TODO 显示表格信息 private void displayRegeditedInfo() {  Iterator        iterator = iterable.iterator();  ICells          iCells = GlobalVariable.manager    .createPersonDataCells(IInspectionManager.CS_PERSON_LIST_CELLS);  boolean flag = true;// 标题栏为true,内容栏位false  int colorChange = 1;// 用来判断单双行,以显示不同的颜色  TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);  tableLayout.setStretchAllColumns(true);  tableLayout.setShrinkAllColumns(true);   while (iterator.hasNext())  {   // 行的样式   TableRow.LayoutParams params = new TableRow.LayoutParams(       ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);   if (flag)// 首先显示表格的标题栏,内容自己定义   {    TableRow titleRow = new TableRow(this);    for (int i = 0; i < colums; i++)// 列数    {// 列名     params.setMargins(1, 1, 1, 1);     TextView textView = new TextView(this);     textView       .setBackgroundColor(getResources().getColor(R.color.top));     textView.setTextColor(Color.WHITE);     textView.setTextSize(31);     textView.setLayoutParams(params);     textView.setText(columsName);// 列名     textView.setTextSize(30);     textView.setGravity(Gravity.CENTER_HORIZONTAL);     titleRow.addView(textView);// 把控件添加到行TableRow中    }    flag = false;    tableLayout.addView(titleRow);// 把行添加到TableLayout中   }    // 新建一行,显示每个成员的具体信息   TableRow personRow = new TableRow(this);   for (int i = 0; i < lines; i++)   {    params.setMargins(1, 1, 1, 1);    object; // 我在这里用Object代表表格显示的内容,      // Object可以是字符串、数字,也可以是照片,看你具体的定义    if (object instanceof String)    {// 字符串居中显示     TextView textView = new TextView(this);     textView.setLayoutParams(params);     textView.setTextSize(29);     if (colorChange % 2 == 1)      textView.setBackgroundColor(getResources().getColor(        R.color.second));     else      textView.setBackgroundColor(getResources().getColor(        R.color.third));     textView.setText(object.toString());     textView.setTextSize(30);     textView.setGravity(Gravity.CENTER);     personRow.addView(textView);    }     else if (object instanceof Number)    {// 数字居右显示     TextView textView = new TextView(this);     textView.setPadding(0, 0, 5, 0);// 右内边距     textView.setLayoutParams(params);     textView.setText(object.toString());     textView.setTextSize(30);     textView.setTextSize(29);     if (colorChange % 2 == 1)      textView.setBackgroundColor(getResources().getColor(        R.color.second));     else      textView.setBackgroundColor(getResources().getColor(        R.color.third));     textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);     personRow.addView(textView);    }     else if (object instanceof byte[])    {// 显示头像     TableRow.LayoutParams params2 = new TableRow.LayoutParams(60, 75);     ImageView imageView = new ImageView(this);     if (colorChange % 2 == 1)      imageView.setBackgroundColor(getResources().getColor(        R.color.second));     else      imageView.setBackgroundColor(getResources().getColor(        R.color.third));     Bitmap bitmap = BitmapFactory.decodeByteArray((byte[]) object,       0, ((byte[]) object).length);     imageView.setImageBitmap(bitmap);     imageView.setLayoutParams(params2);     personRow.addView(imageView);    }     else    {// 空值     TextView textView = new TextView(this);     textView.setLayoutParams(params);     textView.setTextSize(30);     if (colorChange % 2 == 1)      textView.setBackgroundColor(getResources().getColor(        R.color.second));     else      textView.setBackgroundColor(getResources().getColor(        R.color.third));     personRow.addView(textView);    }   }   colorChange++;   tableLayout.addView(personRow);  } }

还可以对整个布局、整行或某个空间添加监听事件,只需setId(int id),然后在设立监听器即可。

以上是"Android如何使用代码动态生成界面"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

内容 布局 表格 代码 动态 界面 生成 信息 字符 字符串 控件 数字 最好 标题 程序 篇文章 面的 监听 不同 方便快捷 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技垄断 与互联网科技公司 广州互联网科技公司 互联网金融与金融科技 易互联网科技有限公司 狮子座互联网科技 新浪科技互联网 佰商互联网科技有限公司 互联网教育科技公司vipkid 银川新华互联网科技职业技术培训学校图片 科技与互联网哪个更好 成都铂石互联网科技做什么的 互联网科技与管理 互联网公司黑科技创始人 佛山市拉法互联网科技有限公司 沐思想互联网科技有限公司 上海同邻互联网科技有限公司规模 普陀区互联网教育科技管理办法 10位科技互联网大佬 黑龙江互联网科技公司 北京禾田雨橡互联网科技 万达互联网科技 江苏南京365互联网科技 来此购互联网科技有限公司 朱啸虎曾投资哪些互联网科技企业 山东有哪些互联网科技公司 国大互联网教育科技有限公司薪资 上海乐五互联网科技有限公司 同城互联网络科技 银川新华互联网科技学校原画
0