将图像分配到列中

时间:2012-10-06 15:53:00

标签: android

这可能是一个非常简单的问题,但我找不到任何解决方案。我的要求是这样的。给定的图像数量和行数,图像应该在行中均匀分布。我在做什么..但现在问题在于最糟糕的情况:Ex。如果我将图像总数设为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);
    }
}

1 个答案:

答案 0 :(得分: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);
}    
}