我正在尝试在我的应用程序中手动设置图像视图位置,代码当前正在工作,但所有的Facebook图像显示在右上方并且彼此叠加。
我无法访问任何适配器,这是在Facebook SDK中,任何人都可以通过以下代码发现任何问题:
我的XML:
DisplayPhotos.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);
}
答案 0 :(得分:1)
答案 1 :(得分:0)
我发现了一个技巧,可以将imageview放在相对布局内的绝对位置xy。下面的代码将imageview的CENTER放在x,y处,甚至在relativelayout边界之外。
/*
* 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);
}