For loop android中的嵌套循环

For loop android中的嵌套循环,for-loop,For Loop,我有一系列的图像。 我需要在一行中显示3个图像,然后在另一行中显示下一个图像。 如何使用嵌套for循环执行此操作。有人能帮我吗? 谢谢。您需要两个循环。 外部循环用于行。行中的每个元素对应一列,因此内部循环用于列。其简单之处在于,与3列一起使用 <GridView android:layout_height="wrap_content" android:id="@+id/gridView1" android:layout_width="mat

我有一系列的图像。 我需要在一行中显示3个图像,然后在另一行中显示下一个图像。 如何使用嵌套for循环执行此操作。有人能帮我吗? 谢谢。

您需要两个循环。 外部循环用于行。行中的每个元素对应一列,因此内部循环用于列。

其简单之处在于,与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">


基本嵌套循环结构如下所示:

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);
    }
}
int-imageIndex=0;
对于(int row=0;row