在Android中正确显示TableLayout中的图像

时间:2011-04-26 18:15:07

标签: android android-layout layout imageview tablelayout

我遇到了问题,我的图像没有在我以编程方式创建的Tablelayout中正确缩小。

This is what I'm currently getting

这是我的代码:

final TableLayout grd_overview = (TableLayout)this.findViewById(R.id.tbl_overview);
grd_overview.setShrinkAllColumns(true);

Integer[] mThumbIds = { R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1,  R.drawable.memory_2, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1,R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1, R.drawable.memory_2, R.drawable.memory_1};

int x = 0;
for (int j = 0; j <= 5; j++) {

    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        for (int i = 0; i <= 5; i++) {
            ImageView iv = new ImageView(this);
            iv.setImageResource(mThumbIds[x]);
            tr.addView(iv);
            x++;
        }
    grd_overview.addView(tr);            
}

XML:

<TableLayout
      android:id="@+id/tbl_overview"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />  

我不知道如何缩小所有ImageView完美匹配的行。底部屏幕截图上的图像比它们应该的要小。

1 个答案:

答案 0 :(得分:0)

您可以在ImageView周期内初始化每个for后添加以下代码行:

iv.setScaleType(ScaleType.FIT_CENTER);

或者选择最适合您需求的缩放类型。

您可以阅读有关ScaleTypes here以及scaling matrice algorithms的更多信息,例如:这是FIT_CENTER的作用。