0

它只显示 1 列。我想在表中动态添加 2 列,而不在 tableRow 中使用另一个 for 循环。它只显示 1 列。我想在表中动态添加 2 列,而不在 tableRow 中使用另一个 for 循环。

        **for (int j = 0; j <= arrtype.size() - 1; j++) {
        row = new TableRow(this);
        TableRow.LayoutParams trParams = (new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        String arrnameHolder = arrname.get(j);
        int imgId = getResources().getIdentifier(arrimage.get(j),
                "drawable", "com.package");
        RelativeLayout rl = new RelativeLayout(this);
        RelativeLayout.LayoutParams rlParams = (new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        rlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);        

        img = new ImageView(this);
        img.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        img.setPadding(10, 10, 10, 10);
        img.setImageResource(imgId);
        tv = new TextView(this);
        tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        tv.setText(arrnameHolder);
        tv.setTextColor(Color.WHITE);
        // tv.setBackgroundColor(Color.parseColor("#7000"));
        tv.setLayoutParams(rlParams);
        tv.setBackgroundColor(color.background_dark);
        tv.setTypeface(Typeface.DEFAULT_BOLD);
        tv.setTextAppearance(getApplicationContext(),
                android.R.style.TextAppearance_Large);
        tv.setGravity(Gravity.CENTER);
        tv.setPadding(10, 10, 10, 10);
        rl.addView(img);
        rl.addView(tv);
        row.addView(rl);

        table.addView(row);
    }
    scroll.addView(table);
    layout.addView(scroll, scrollParam);
}**
4

1 回答 1

1

而不是RelativeLayout在表格行中,使用LinearLayout.

此外,无需编写rlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);此行所做的是,将最新项目添加到表格底部...

于 2013-04-27T08:47:38.527 回答