0

我正在尝试在我的应用程序中手动设置图像视图位置,代码当前正在运行,但所有 facebook 图像都显示在右上角并且彼此重叠。

我无权访问任何适配器,这是在 Facebook SDK 中,任何人都可以发现以下代码的任何问题:

我的 XML:

显示照片.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DisplayPhotosLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

这是由以下设置的:

fbManager = new FBLoginManager(this, R.layout.displayphotos, "-------", permissions);

然后我使用以下内容显示图像(工作但都在同一位置)

int Row = 0;
    int RowCount = 0;
    int imageWidth = (int)getWindowManager().getDefaultDisplay().getWidth() / 3;

    for(int _i = 0; _i < _noOfPhotos; _i++)
    {
        ImageView imageView = new ImageView(getApplicationContext());
        imageView.setImageResource(R.drawable.no_image);

        RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(imageWidth, imageWidth);
        rl.setMargins(imageWidth * _i, imageWidth * Row, 0,0);

        RowCount = RowCount + 1;

        if(RowCount == 3){Row = Row + 1;RowCount = 0;}

        UrlImageViewHelper.setUrlDrawable(imageView, _userGallery.get(_i).getPicture());

        addContentView(imageView, rl);

        System.out.println(imageWidth * _i + " top: " + imageWidth * Row);

        _imageViewArray.add(imageView);
    }
4

2 回答 2

1

我认为你需要一个表格布局,http://developer.android.com/guide/topics/ui/layout/grid.html

于 2012-07-14T02:49:25.567 回答
0

我找到了一个技巧,可以将图像视图放置在相对布局内的绝对位置 xy 处。以下代码将 imageview 的 CENTER 放置在 x,y 处,甚至在相对布局边界之外。

    /*
     * relativelayout allow out-of-boudaries only along the alignment side.
     * if aligned to left then you can have a negative x and overlap the
     * left side, if aligned to right you can overlap the right
     * 
     * let's put the image CENTERED on x,y
     * 
     * If we are on the left side of the container 
     *      |  (x<dx/2) |<---imgDx-->|
     *      |           |____________|
     *      |
     *      |      x 
     *      |<---------------->                  |
     *      |<----------> x-imgDx/2              |
     *      |____________________________________|
     * 
     * if we goes past the half of the container
     *                        <---imgDx-->       |
     *      |                |____________|      |
     *      |           x                        |
     *      |<--------------------->             |
     *      |                             <----->| dx-(x+imgDx/2) 
     *      |____________________________________|
     */
    public void setBitmapPosition(ImageView iv, int x, int y) {
        String log = "";
        RelativeLayout rl = (RelativeLayout) iv.getParent();

        // get bitmap size
        int imgDx = iv.getLayoutParams().width;
        int imgDy = iv.getLayoutParams().height;
        // get container size
        int dx = rl.getWidth();
        int dy = rl.getHeight();

        log = log + " XY:" + new Integer(x).toString() + ","
                + new Integer(y).toString();

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                imgDx, imgDy);
        iv.setLayoutParams(lp);

        log = log + " imgXY:" + new Integer(imgDx).toString() + ","
                + new Integer(imgDy).toString();

        log = log + " winXY:" + new Integer(dx).toString() + ","
                + new Integer(dy).toString();

        if (x <= (dx / 2)) {
            // i'm on the left side of the view so let's align to left
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
                    RelativeLayout.TRUE);
            lp.leftMargin = x - imgDx / 2;
            log = log + " LEFT:" + new Integer(lp.leftMargin).toString();
        } else {
            // align to right. we are past the middle
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
                    RelativeLayout.TRUE);
            lp.rightMargin = dx - (x + imgDx / 2);
            log = log + " RIGHT:" + new Integer(lp.rightMargin).toString();
        }

        if (y <= (dy / 2)) {
            // align to top
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            lp.topMargin = y - imgDy / 2;
            log = log + " TOP:" + new Integer(lp.topMargin).toString();

        } else {
            // align to bottom
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
                    RelativeLayout.TRUE);
            lp.bottomMargin = dy - (y + imgDy / 2);
            log = log + " BOTTOM:"
                    + new Integer(lp.bottomMargin).toString();
        }

        iv.setLayoutParams(lp);

        Log.i("PARAM", log);
    }
于 2013-06-27T18:37:55.563 回答