2011-10-14 119 views
0

我有一个图像数组。 我需要在一行中显示3个图像,然后在另一行显示下一个图像。 如何使用嵌套for循环执行此操作。任何人都可以帮助我吗? 谢谢。在android中嵌套循环

回答

0

基本嵌套的循环结构是这样的:

int imageIndex = 0; 
for (int row = 0; row < rowCount; row++) { 
    for (int column = 0; column < columnCount; column++ { 
     // Draw your image here at x position of (column * image width) 
     // and y position of (row * image height). Add a bit to each if you 
     // want some spacing between your images. 
     // For example: 
     drawMyImage(images[imageIndex++], column * imageWidth, row * imageHeight); 
    } 
} 
0

您需要两个循环。 外循环用于行。行中的每个元素都对应一列,所以内部循环是针对列的。

1

其简单,使用GridView 3列。

<GridView 
     android:layout_height="wrap_content" 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:numColumns="3" 
     android:horizontalSpacing="10dp" 
     android:verticalSpacing="10dp">