使用FrameLayout更改绝对位置

时间:2011-08-15 17:03:33

标签: android android-layout

我有一个应该显示在某些图像上的特定位置的视图,我已经使用AbsoluteLayout做了,但我不想使用它,我试图用FrameLayout获得相同的结果。< / p>

但我无法解决这个问题,这是我的两次尝试:

public void showVideo(RectF bounds) {
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) video.getLayoutParams();
    params.width = (int) bounds.width();
    params.height = (int) bounds.height();
    int x = (int) viewer.getPageXOffset();
    int y = (int) viewer.getPageYOffset();

    video.setPadding((int) (x + bounds.left), (int) (y + bounds.top), (int) (x + bounds.right),
            (int) (y + bounds.bottom));

    video.setLayoutParams(params);
    video.invalidate();

    video.setVisibility(View.VISIBLE);
    video.setFocusable(true);
    video.setFocusableInTouchMode(true);
    video.requestFocus();
}

public void showVideo(RectF bounds, final String videoUrl) {
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) video.getLayoutParams();
    params.width = (int) bounds.width();
    params.height = (int) bounds.height();
    int x = (int) (viewer.getPageXOffset() + bounds.left);
    int y = (int) (viewer.getPageYOffset() + bounds.top);

    params.leftMargin = x;
    params.topMargin = y;

    video.setLayoutParams(params);
    video.invalidate();

    video.setVisibility(View.VISIBLE);
    video.setFocusable(true);
    video.setFocusableInTouchMode(true);
    video.requestFocus();
}

但在这两种情况下,VideoView都显示在v(0,0)(左上角)。

1 个答案:

答案 0 :(得分:17)

在LayoutParams中设置重力时应该可以正常工作。

尝试

params.gravity = Gravity.LEFT | Gravity.TOP;

开始。