0

我想创建一个包含图像的布局,我想在其上放置许多图像和 TextView。我知道如何使用 RelativeLayout 将图像放在另一个之上,但是如何以所需的方式对齐它们?例如,我希望图像恰好位于我的“背景”图像具有特定黑色圆圈的位置。使用 android:layout_marginTop 等值似乎并不能在每个屏幕上产生效果。

那么处理这些问题的正确方法是什么?

编辑:我无法上传图像,但我在这里上传了我想要的非常简单的草图:http: //imageshack.us/photo/my-images/715/buttonlayout.png/ 所有按钮也有图标和文本(这必须是一个文本视图,以便我可以在需要时以编程方式更改它)

4

1 回答 1

1

您必须创建一个自定义布局,将图像专门放置在您想要它们相对于父视图大小的位置。如果您选择,您可以覆盖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);
}
于 2012-06-26T14:36:31.980 回答