将表格行添加到表格布局的问题

时间:2011-11-27 05:44:24

标签: java android tablelayout tablerow

我有以下代码:

HashMap<String, String> tip = venue.tips.get(j);
            TableRow tr = new TableRow(this);
            TableRow.LayoutParams tableRowParams =
                  new TableRow.LayoutParams
                  (TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
            tr.setLayoutParams(tableRowParams);

            LinearLayout ll = new LinearLayout(this);
            ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            TextView tips = new TextView(this);
            tips.setText(tip.get("text"));
            Log.v("TIPS TEXT", tip.get("text"));
            tips.setLayoutParams(new LayoutParams(
                     LayoutParams.WRAP_CONTENT,
                     LayoutParams.WRAP_CONTENT));

            ImageView avatar = new ImageView(this);
            avatar.setImageBitmap(getBitmapFromURL(tip.get("photo")));
            Log.v("PHOTO URL", tip.get("photo"));
            avatar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            ll.addView(tips);
            ll.addView(avatar);

            tr.addView(ll);

            View v = new View(this);
            v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
            v.setBackgroundColor(Color.rgb(51, 51, 51));

            tipsTable.addView(tr,new TableLayout.LayoutParams(
                      LayoutParams.FILL_PARENT,
                      LayoutParams.WRAP_CONTENT));
            tipsTable.addView(v);

我认为我在设置布局时遇到了问题,但现在的问题是我看不到添加到tipsTable的行。这是为什么?

2 个答案:

答案 0 :(得分:0)

您并未在任何地方致电setContentView()。您创建此布局但从未将其分配给活动。我想你想要做的就是在你设置了所有布局之后:

setContentView(ll);

答案 1 :(得分:0)

沿着同一行:tipsTable似乎是你的顶视图所以尝试setContentView(tipsTable)