首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态创建表格布局android

动态创建表格布局android
EN

Stack Overflow用户
提问于 2010-09-30 21:10:48
回答 1查看 1.2K关注 0票数 3

我想动态地向表布局中添加行。这是我的代码。

代码语言:javascript
复制
 TableRow tableRow = null;
 TextView textView = null;
 ImageView imageView = null;
 TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout);
 RelativeLayout relativeLayout = null;
         for (String string: listOfStrings) {
                            tableRow = new TableRow(this);
                            relativeLayout = (RelativeLayout) View.inflate(this,
                                    R.layout.row, null);
                            textView = (TextView) relativeLayout.getChildAt(0);
                            textView.setText(string);
                            imageView= (ImageView) relativeLayout.getChildAt(1);
                            imageView.setBackgroundColor(R.color.blue);
                            tableRow.addView(relativeLayout);
                            tableLayout.addView(tableRow);
                        }

我已经创建了一个宽度为fill_parent的行布局,textView在最左侧,imageView在最右侧。但是,当我运行这个程序时,行宽显示为wrap_content,而不是带有图像重叠文本的fill_parent。请帮帮忙。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-03 02:16:53

我注意到的第一件事是在添加视图时没有设置任何LayoutParams。

代码语言:javascript
复制
TableRow tableRow = null;
TextView textView = null;
ImageView imageView = null;
TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout);
RelativeLayout relativeLayout = null;

for (String string: listOfStrings)
{
    tableRow = new TableRow(this);
    relativeLayout = (RelativeLayout) View.inflate(this, R.layout.row, null);
    textView = (TextView) relativeLayout.getChildAt(0);
    textView.setText(string);
    imageView= (ImageView) relativeLayout.getChildAt(1);
    imageView.setBackgroundColor(R.color.blue);

    //Here's where I would add the parameters...
    TableLayout.LayoutParams rlParams = new TableLayout.LayoutParams(FILL_PARENT, WRAP_CONTENT);
    tableRow.addView(relativeLayout, rlParams);
    tableLayout.addView(tableRow);
}

在实践中,我还会将行的创建抽象为它们自己的方法。以简化使用/重用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3830791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档