Android使用代码动态生成界面

 更新时间:2021年09月18日 11:20:10   作者:阿牛哞了一声  
这篇文章主要为大家详细介绍了Android使用代码动态生成界面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableLayout>
 
</LinearLayout>

在代码中新建一个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  LayoutInflater.inflate源码分析

    Android LayoutInflater.inflate源码分析

    这篇文章主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下
    2016-12-12
  • Android自定义TitleView标题开发实例

    Android自定义TitleView标题开发实例

    这篇文章主要介绍了Android自定义TitleView标题开发实例的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Android可循环显示图像的Android Gallery组件用法实例

    Android可循环显示图像的Android Gallery组件用法实例

    这篇文章主要介绍了Android可循环显示图像的Android Gallery组件用法,结合实例形式分析了Gallery组件的功能,使用方法及相关注意事项,需要的朋友可以参考下
    2016-04-04
  • Android中Service和Activity相互通信示例代码

    Android中Service和Activity相互通信示例代码

    在android中Activity负责前台界面展示,service负责后台的需要长期运行的任务。下面这篇文章主要给大家介绍了关于Android中Service和Activity相互通信的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-09-09
  • 详解Android studio 3+版本apk安装失败问题

    详解Android studio 3+版本apk安装失败问题

    这篇文章主要介绍了详解Android studio 3+版本apk安装失败问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Android实现简单实用的搜索框

    Android实现简单实用的搜索框

    这篇文章主要为大家详细介绍了Android实现简单实用的搜索框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • 在Android中使用WebSocket实现消息通信的方法详解

    在Android中使用WebSocket实现消息通信的方法详解

    这篇文章主要介绍了在Android中使用WebSocket实现消息通信的方法详解,消息推送功能可以说移动APP不可缺少的功能之一,使用WebSocket实现消息推送功能。感兴趣的可以了解一下
    2020-07-07
  • android实现加载动画对话框

    android实现加载动画对话框

    这篇文章主要为大家详细介绍了android实现加载动画对话框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • 浅析Android 的 MediaPlayer类

    浅析Android 的 MediaPlayer类

    本文主要介绍了Android的mediaplayer类作用和用法,并附上了关键代码,有需要的朋友可以参考下
    2014-10-10
  • 一文带你深入理解Android Window系统

    一文带你深入理解Android Window系统

    Android中的窗口系统是应用程序用户界面的核心组件之一,它负责管理可视化区域、处理用户输入事件以及与系统UI交互,本文将深入介绍与Android窗口系统相关的重要概念,需要的朋友可以参考下
    2023-10-10

最新评论