2011-01-12 65 views
1

我有一個TableLayout元素在我的main.xml如何在android jdk中動態地用ImageView填充TableLayout?

<TableLayout android:id="@+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TableLayout> 

我不想硬編碼ImageViewmain.xml

如何嵌入ImageView.java

+0

你可以看看Android開發者官方網站的教程之一,特別是這一個:http://developer.android.com/resources/tutorials/views/hello-gridview.html。它使用`GridView`而不是`TableLayout`,但是展示瞭如何在Java代碼中挑選圖像和填充`ImageView`實例。 – 2011-01-12 07:04:05

回答

4

希望這會有所幫助。

  TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); 
      for (int r=1; r<=rowCount; r++){ 
       TableRow tr = new TableRow(this); 
       for (int c=1; c<=columnCount; c++){ 
        ImageView im = new ImageView (this); 
        im.setImageDrawable(getResources().getDrawable(R.drawable.image_name)); 
        im.setPadding(0, 0, 0, 0); //padding in each image if needed 
        //add here on click event etc for each image... 
        //... 
        tr.addView(im, imageWidth,imageHeight); 
       } 
       table.addView(tr); 
      }