【问题标题】:How to create table with dynamic number of rows in Android如何在Android中创建具有动态行数的表
【发布时间】:2013-07-28 21:56:14
【问题描述】:

我正在尝试使用基于图标数组的动态行数填充 TableLayout,但是我无法让行显示在屏幕上。该表应显示一个 5x3 的图标网格。我使用的代码如下:

    Services serv = new Services();

    Item[] iconArray = serv.getIcons();

    TableLayout table = (TableLayout) findViewById(R.id.table1);

    int rowNum = iconArray.length/5;
    TableRow rows[]= new TableRow[rowNum];

    int count = 0;
    for (int i = 0; i < iconArray.length; i++) { 
        final String name = iconArray[i].name; 
        final int icon = iconArray[i].icon;

        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View v = inflater.inflate(R.layout.icon, null,false);
        TextView text = (TextView) v.findViewById(R.id.text);
        text.setText(name);

        ImageView imageicon = (ImageView) v.findViewById(R.id.icon);
        imageicon.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Do some stuff
            }
        });
        Drawable drawicon = getResources().getDrawable(icon);
        imageicon.setBackgroundDrawable(drawicon);

        if(i == 4) //End of Row
        {   
            count++;
        }
        if(i == 9) //End of Row2
        {   
            count++;
        }

        rows[count] = new TableRow(this);
        rows[count].addView(v);
        table.addView(rows[count]);
    }

这不会向屏幕输出任何内容,并且 TableLayout 保持为空。谁能告诉我我的思路是否正确?

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 您能解释一下您的 TableLayout 必须显示的内容吗?只有 Image 和 TextView 的行?
  • @Jahckyto TableLayout 应该显示三行填充图标和一个带有每个图标名称的 TextView。出于其他原因,此处不能选择使用 GridView

标签: android icons row tablelayout


【解决方案1】:

首先,我将为我的行做一个特定的布局:

my_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:weightSum="1" >

    <ImageView
        android:id="@+id/myImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"" />

</TableRow>

然后,我将简单地用我的行循环填充我的表格:

static TableRow row;
Item[] iconArray = serv.getIcons();
TableLayout table = (TableLayout) myView.findViewById(R.id.table1);

for (int i = 0; i < iconArray.length; i++) {

    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    row = (TableRow) inflater.inflate(R.layout.my_row, cont, false);

    final String name = iconArray[i].name; 
    final int icon = iconArray[i].icon;

    TextView text = (TextView) v.findViewById(R.id.myText);
    text.setText(name);

    ImageView imageicon = (ImageView) v.findViewById(R.id.myImg);
    imageicon.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Do some stuff
        }
    });
    Drawable drawicon = getResources().getDrawable(icon);
    imageicon.setBackgroundDrawable(drawicon);

    table.addView(row);
}

【讨论】:

  • 感谢您的回答,但我相信这会为每个图标创建一个新行。我需要的是 5x3 网格中的 3 行。这是我的错,我在最初的问题中没有说清楚
  • 是的,它为每个图像创建一行。不知道能不能做一个GridLayout的表格,这样的话你画表格就方便了
猜你喜欢
  • 1970-01-01
  • 2018-12-02
  • 2021-06-26
  • 2012-01-13
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
相关资源
最近更新 更多