2012-06-16 21 views
0

我想要一個2列ScrollView。在每一列中,應該有一個ImageButton如何使用兩列ScrollView?

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollView1" 
android:layout_height="800dp" 
android:background="#FFF" 
android:layout_width="600dp" > 

<LinearLayout 
    android:id="@+id/categoryLinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</LinearLayout> 

</ScrollView> 

,代碼:

 LinearLayout sv = (LinearLayout) findViewById(R.id.categoryLinearLayout1); 

    for (int i = 0; i < 10; i++) { 
     ImageButton ib = new ImageButton(this); 
     // ib.setImageDrawable(getResources().getDrawable(R.drawable.cat1)); 

     Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
       R.drawable.cat1); 
     int width = 300; 
     int height = 300; 
     Bitmap resizedbitmap = Bitmap.createScaledBitmap(bmp, width, 
       height, true); 
     ib.setImageBitmap(resizedbitmap); 
     sv.addView(ib); 
    } 

但這種方式,所有10 ImageButtons水平。我需要的是,把2 ImageButton連成一排(它使得600px)並放下,放更多2 ImageButtons等。所以將會有5行10 ImageButtons

我該怎麼做?

+0

如果你希望他們獨立滾動,那麼你就需要使用兩個ScrollViews – FoamyGuy

+0

按找你要求它看起來2列的網格視圖也是一個不錯的選擇... http://developer.android.com/resources/tutorials/views/hello-gridview.html –

+0

是的,GridView似乎是最好的選擇。謝謝 – Burak

回答

1

使用TableLayout

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollView1" 
android:layout_height="800dp" 
android:background="#FFF" 
android:layout_width="600dp" > 

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

</ScrollView> 

然後在你的代碼:

TableLayout sv = (TableLayout) findViewById(R.id.categoryLinearLayout1); 

for (int i = 0; i < 5; i++) { 
    TableRow tr = new TableRow(this); 
    tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); 
    for (int j = 0; j < 2; j++) { 
     ImageButton ib = new ImageButton(this); 
     // ib.setImageDrawable(getResources().getDrawable(R.drawable.cat1)); 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
       R.drawable.cat1); 
     int width = 300; 
     int height = 300; 
     Bitmap resizedbitmap = Bitmap.createScaledBitmap(bmp, width, 
      height, true); 
     ib.setImageBitmap(resizedbitmap); 
     ib.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)); 
     tr.addView(ib); 
    } 
    sv.add(tr); 
}