0

我正在尝试RelativeLayout在运行时将一系列图像添加到当前的另一个TextView. 到目前为止,我让它显示部分正确,但不完全正确。我不能让他们搬到另一排。我希望有人能帮我一把,告诉我正确的方法。该系列图像将出现在此TextView( R.id.date) 下方:

     TextView date = (TextView) findViewById(R.id.date);

    //// image view start //////
    int photos = Integer.parseInt(total_photo);
    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relative_layout_b);
    for (int i = 0; i < limit; i++){
        final ImageView imageView = new ImageView (this);
        imageView.setId(i);
        imageView.setImageResource(R.drawable.photo_frame);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageView.setPadding(10, 10, 0, 0);
        imageView.setAdjustViewBounds(true);
        imageView.setMaxHeight(80);
        imageView.setMaxWidth(80);
        lp.addRule(RelativeLayout.BELOW, R.id.date);
        lp.addRule(RelativeLayout.RIGHT_OF, imageView.getId() - 1);
        imageView.setLayoutParams(lp);
        mainLayout.addView(imageView);
    }

目前只显示总照片数量 - 1(即:有5张时,只显示4张);我想让每一行显示 5,如果达到 6、11、16....等,我会立即移动到下一行。此布局嵌套在 aScrollView和 a 中,RelativeLayout因为其中有很多视图。所以,我将不得不坚持RelativeLayout这一点。

4

1 回答 1

1

如果我理解你想要做什么,看看下面的代码是否ImageViews像你想要的那样(我不知道它的效率如何):

    private static final int ROW_ITEMS = 5; // 5 ImageViews per row

    // ...
    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relative_layout_b);
            int limit = 13; // I assume that limit is the number of ImageView that you'll put in the layout
            int rows = limit / ROW_ITEMS; // the number of rows that results from limit
            int leftOver = limit % ROW_ITEMS; // see if we have incomplete rows
            if (leftOver != 0) {
                rows += 1;
            }
            int id = 1000; // the ids of the ImageViews 1000, 1001, 1002 etc
            int belowId = R.id.date; // this id will be used to position the ImageView on another row
            while (rows > 0) {
                int realItemsPerRow = ROW_ITEMS;
                if (leftOver != 0 & rows == 1) {
                    realItemsPerRow = Math.min(ROW_ITEMS, leftOver);
                }           
                for (int i = 0; i < realItemsPerRow; i++) {
                    final ImageView imageView = new ImageView(this);
                    imageView.setId(id);
                    imageView.setImageResource(R.drawable.ic_launcher);
                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
                    imageView.setPadding(10, 10, 0, 0);
                    imageView.setAdjustViewBounds(true);
                    imageView.setMaxHeight(80);
                    imageView.setMaxWidth(80);
                    if (i == 0) {
                        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                    } else {
                        lp.addRule(RelativeLayout.RIGHT_OF, imageView.getId() - 1);
                    }
                    lp.addRule(RelativeLayout.BELOW, belowId);              
                    imageView.setLayoutParams(lp);
                    mainLayout.addView(imageView);
                    id++;
                }           
                belowId = id - 1; 
                rows--; 
            }

此外,正如 kcoppock 在他的评论中已经说过的那样,可能值得关注GridView效率。

于 2012-07-20T15:38:56.423 回答