2014-07-16 63 views
0

我正試圖在每張圖像下方的圖像和搜索欄上執行表格。以編程方式顯示圖像和按鈕的表格行

在我layout.xml:

<TableLayout 
    android:id="@+id/table" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:shrinkColumns="*" 
    android:stretchColumns="*" > 

</TableLayout> 

林我Main.java我做循環:

// outer for loop 
for (int i = 1; i <= rows; i++) { 

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

    // inner for loop 
    for (int j = 1; j <= cols; j++) { 

     // Picture 
     ImageView image = new ImageView(this); 
     image.setImageResource(R.drawable.like);     
     image.setPadding(5, 5, 5, 5); 
     image.setId(1);    

     row.addView(image); 

     // Seekbar 
     SeekBar seekBar = new SeekBar(this); 
     seekBar.setId(2); 

     RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     p.addRule(RelativeLayout.BELOW, image.getId()); 

     Log.d("BMT", Integer.toString(image.getId())); 

     row.addView(seekBar, p); 

    } 

    table.addView(row); 

} 

這樣做的結果就是與圖片桌子和無覓吧!我嘗試了很多組合,在stackoverflow中閱讀帖子,但我想我錯過了一些東西。請幫助我瞭解我做錯了什麼。我想也許Row也應該是RelativeLayout,但它不起作用。

+0

直接包含在'TableRow'中的任何'View'都需要使用'TableRow.LayoutParams'。另外,您添加行的方式將不起作用。情侶選項:1)將'ImageView'和'SeekBar'放入自己的'Layout'中,然後將'Layout'添加到'TableRow'。 2)在添加一行'ImageView's和一行'SeekBar'之間交替。 –

回答

0

試試這種方式,希望這會幫助你解決你的問題。

for (int i = 1; i <= rows; i++) { 
    TableRow row = new TableRow(this); 
    row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    // inner for loop 
    for (int j = 1; j <= cols; j++) { 
     // Picture 
     ImageView image = new ImageView(this); 
     image.setImageResource(R.drawable.like); 
     image.setPadding(5, 5, 5, 5); 
     image.setId(1); 

     RelativeLayout relativeLayout = new RelativeLayout(this); 
     RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     relativeLayout.setLayoutParams(p); 
     relativeLayout.addView(image); 

     // Seekbar 
     SeekBar seekBar = new SeekBar(this); 
     seekBar.setId(2); 

     p.addRule(RelativeLayout.BELOW, image.getId()); 
     Log.d("BMT", Integer.toString(image.getId())); 

     relativeLayout.addView(seekBar); 
     row.addView(relativeLayout); 

    } 
} 
+0

我做到了,應用程序現在還沒開始:'07 -16 20:16:37.541:E/AndroidRuntime(3455):致命例外:主要 07-16 20:16:37.541:E/AndroidRuntime(3455):java .lang.ArithmeticException:除以0 07-16 20:16:37.541:E/AndroidRuntime(3455):\t at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:587)' ' – Pasha

+0

無論如何謝謝!我終於做到了! – Pasha

相關問題