您必须创建一个自定义布局,将图像专门放置在您想要它们相对于父视图大小的位置。如果您选择,您可以覆盖LayoutParams
并应用自定义属性以供您的自定义视图读取。
无论如何,要专门放置一个项目,例如从顶部向下 30% 和从左侧向下 20%,您将覆盖onLayout()
.
@Override
public void onLayout(boolean c, int left, int top, int right, int bottom) {
super.onLayout(c, left, top, right, bottom);
int width = right - left;
int height = bottom - top;
View v = getTheChildView();
int viewL = left + (int)(width * .2f); // The left pixel is 20% down the total width of the parent view
int viewR = viewL + v.getWidth(); // The right pixel is the left pixel plus the measured width of the child view itself
int viewT = top + (int)(height * .3f); // The top pixel is 30% down the total height of the parent view
int viewB = top + v.getHeight(); // The bottom pixel is the top pixel plus the measured height of the child view itself
v.layout(viewL, viewT, viewR, viewB);
}