【问题标题】:Adding an ImageView array to a RelativeLayout将 ImageView 数组添加到 RelativeLayout
【发布时间】:2012-07-20 14:45:48
【问题描述】:

我正在尝试在运行时将一系列图像添加到当前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 等,我会立即移动到下一行。这个布局嵌套在ScrollViewRelativeLayout 中,因为我有很多视图。所以,为此我将不得不坚持使用RelativeLayout

【问题讨论】:

  • 我建议将 GridView 作为您的父布局,并将您的 RelativeLayout 设置为视图以膨胀到您的 GridView。

标签: android android-layout android-relativelayout android-imageview


【解决方案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

【讨论】:

  • 如果我想在这些图片下面添加一个文本视图怎么办?下面..?
  • @SArumik 在最后一行图像下方?在每行图像下方?在每张图片下方?
  • @SArumik 在while 循环之后,设置TextView,设置所需的RelativeLayout.LayoutParams 并使用规则:relativeParams.addRule(RelativeLayout.BELOW, belowId - 1);。看看这是不是你想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多