溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android如何使用代碼動態(tài)生成界面

發(fā)布時間:2021-09-24 15:48:20 來源:億速云 閱讀:173 作者:小新 欄目:開發(fā)技術

這篇文章主要介紹Android如何使用代碼動態(tài)生成界面,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

我們最常用使用XML來編寫Android應用程序的UI,這樣的好處是方便快捷可視化,而且維護和修改特別容易,但是它是靜態(tài)的。如果我們要做的程序的界面是固定的,用XML固然是最好的選擇,但是如果我們需要動態(tài)、靈活地控制UI,使用代碼來動態(tài)生成UI無疑使最好的辦法。

在XML中,我們使用的五大布局:LinearLayout(線性布局)、RelativeLayout(相對布局)、TableLayout(表格布局)、AbsoluteLayout(絕對布局)和FrameLayout(幀布局)在Android中也有對應的類來表示。

舉個例子,我現(xiàn)在需要顯示一個表格,表格的行數(shù)和列數(shù)及其內(nèi)容都不確定,如果在XML中,這是不可能實現(xiàn)的。

先給大家看一下成品:(下面的代碼只給大家展示如何實現(xiàn),表格里面的內(nèi)容忽略)

Android如何使用代碼動態(tài)生成界面

首先,新建一個不帶任何控件的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,內(nèi)容欄位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)// 首先顯示表格的標題欄,內(nèi)容自己定義
   {
    TableRow titleRow = new TableRow(this);
    for (int i = 0; i < colums; i++)// 列數(shù)
    {// 列名
     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代表表格顯示的內(nèi)容,
      // Object可以是字符串、數(shù)字,也可以是照片,看你具體的定義
    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)
    {// 數(shù)字居右顯示
     TextView textView = new TextView(this);
     textView.setPadding(0, 0, 5, 0);// 右內(nèi)邊距
     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);
  }
 }

還可以對整個布局、整行或某個空間添加監(jiān)聽事件,只需setId(int id),然后在設立監(jiān)聽器即可。

以上是“Android如何使用代碼動態(tài)生成界面”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI