最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用代碼動(dòng)態(tài)生成界面

 更新時(shí)間:2021年09月18日 11:20:10   作者:阿牛哞了一聲  
這篇文章主要為大家詳細(xì)介紹了Android使用代碼動(dòng)態(tài)生成界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

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

首先,新建一個(gè)不帶任何控件的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>

在代碼中新建一個(gè)TableLayout:

// TODO 顯示表格信息
 private void displayRegeditedInfo()
 {
  Iterator
   
     iterator = iterable.iterator();
  ICells
    
      iCells = GlobalVariable.manager
    .createPersonDataCells(IInspectionManager.CS_PERSON_LIST_CELLS);
  boolean flag = true;// 標(biāo)題欄為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)// 首先顯示表格的標(biāo)題欄,內(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中
   }
 
   // 新建一行,顯示每個(gè)成員的具體信息
   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);
  }
 }

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android輪播圖組件的制作方法

    android輪播圖組件的制作方法

    這篇文章主要為大家詳細(xì)介紹了android輪播圖組件的制作方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android原生項(xiàng)目集成React Native的方法

    Android原生項(xiàng)目集成React Native的方法

    本篇文章主要介紹了Android原生項(xiàng)目集成React Native的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android實(shí)現(xiàn)GridView中的item自由拖動(dòng)效果

    Android實(shí)現(xiàn)GridView中的item自由拖動(dòng)效果

    在前一個(gè)項(xiàng)目中,實(shí)現(xiàn)了一個(gè)功能是gridview中的item自由拖到效果,實(shí)現(xiàn)思路很簡(jiǎn)單,主要工作就是交換節(jié)點(diǎn),以及拖動(dòng)時(shí)的移動(dòng)效果,下面小編給大家分享具體實(shí)現(xiàn)過程,對(duì)gridview實(shí)現(xiàn)拖拽效果感興趣的朋友一起看看吧
    2016-11-11
  • Android微信SDK實(shí)現(xiàn)分享

    Android微信SDK實(shí)現(xiàn)分享

    這篇文章主要介紹了Android微信SDK實(shí)現(xiàn)分享的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android實(shí)現(xiàn)搜索保存歷史記錄功能

    Android實(shí)現(xiàn)搜索保存歷史記錄功能

    這篇文章主要介紹了Android實(shí)現(xiàn)搜索保存歷史記錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android實(shí)現(xiàn)簡(jiǎn)單的照相功能

    Android實(shí)現(xiàn)簡(jiǎn)單的照相功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的照相功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android Studio連接手機(jī)設(shè)備教程

    Android Studio連接手機(jī)設(shè)備教程

    這篇文章主要為大家詳細(xì)介紹了Android Studio連接手機(jī)設(shè)備教程,非常完整的連接步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android應(yīng)用開發(fā)中Fragment存儲(chǔ)功能的基本用法

    Android應(yīng)用開發(fā)中Fragment存儲(chǔ)功能的基本用法

    這篇文章主要介紹了Android應(yīng)用開發(fā)中使用Fragment存儲(chǔ)功能的基本用法,包括對(duì)Fragment的非中斷保存setRetaineInstance的講解,需要的朋友可以參考下
    2016-02-02
  • Android編程之繪制文本(FontMetrics)實(shí)現(xiàn)方法

    Android編程之繪制文本(FontMetrics)實(shí)現(xiàn)方法

    這篇文章主要介紹了Android編程之繪制文本(FontMetrics)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android使用FontMetrics對(duì)象繪制文本的相關(guān)技巧,需要的朋友可以參考下
    2015-12-12
  • android APP登陸頁(yè)面適配的實(shí)現(xiàn)

    android APP登陸頁(yè)面適配的實(shí)現(xiàn)

    這篇文章主要介紹了android APP登陸頁(yè)面適配的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09

最新評(píng)論

清徐县| 庆元县| 开远市| 灌云县| 左权县| 株洲市| 库尔勒市| 哈尔滨市| 津南区| 西平县| 桂林市| 阿合奇县| 桦甸市| 南宫市| 陆丰市| 安溪县| 来凤县| 宜宾市| 剑川县| 昌吉市| 收藏| 乐亭县| 武山县| 藁城市| 佛坪县| 延庆县| 甘德县| 雅安市| 师宗县| 启东市| 芷江| 宾阳县| 建湖县| 高阳县| 卢氏县| 金门县| 米脂县| 临沭县| 达拉特旗| 剑川县| 金寨县|