-1

这可能是一个非常简单的问题,但我没有找到任何解决方案。我的要求是这样的。给定图像的数量和行数,图像应该平均分布在行中。我正在这样做..但现在问题在于最坏的情况:例如。如果我给出的图像总数为 99,总行数为 50,那么在 49 行中,列应该是 2,每列一个图像,在第 50 列中,一列应该只有一个图像。我无法找到解决此问题的方法。请帮帮我。我在这里发布代码。请帮我。提前致谢。

    @Override
    protected void onPreExecute() {
        HSC = new HorizontalScrollView(getApplicationContext());
        VSC = new ScrollView(getApplicationContext());
        tableLayout = new TableLayout(getApplicationContext());
        tableLayout.setGravity(Gravity.CENTER);
    } 

    @Override
    protected Void doInBackground(Void... params) {

        runOnUiThread(new Runnable() {
            public void run() {             
                int a =0;
                for (Integer row = 0; row < rows; row++) { //Rows are being plotted.

                    tableRow = new TableRow(getApplicationContext());
                    for (Integer col = 0; col < img; col++) {
                        /*Images shared into number of columns. Column=img/rows*/
                        image = new ImageView(getApplicationContext());
                        android.view.animation.Animation anim = animate(a);
                        /*android.view.animation.Animation anim = AnimationUtils
                                .loadAnimation(getApplicationContext(), R.anim.fadein);*/
                        image.startAnimation(anim);
                        image.setPadding(20, 20, 20, 20);

                        image.setImageResource(R.drawable.images);
                        tableRow.addView(image);
                        a++;
                    }
                    tableLayout.addView(tableRow);
                }
                VSC.addView(tableLayout);
                HSC.addView(VSC);
                setContentView(HSC);
            }
        });

        return null;
    }
    @Override
    protected void onPostExecute(Void result) {  
        super.onPostExecute(result);
    }
}
4

1 回答 1

1

使用两个循环来填充图像。像这样

if (no_of_images % columns == 0) {
    int _rows = no_of_images / columns;
    for (float row = 0; row < _rows; row++) {

    tableRow = new TableRow(getApplicationContext());

    for (int col = 0; col < columns; col++) {

        image = new ImageView(getApplicationContext());
        image.setPadding(20, 20, 20, 20);
        android.view.animation.Animation animation = animate(a);
        image.setAnimation(animation);
        image.setImageResource(R.drawable.images);
        tableRow.addView(image);
        a++;
    }
    tableLayout.addView(tableRow);
}    
}
于 2012-10-09T06:47:54.563 回答