【发布时间】:2011-08-15 17:03:33
【问题描述】:
我有一个视图应该显示在某些图像的特定位置,我已经使用 AbsoluteLayout 做到了,但我不想使用它,我正在尝试使用 FrameLayout 获得相同的结果。
但我无法让它工作,这是我的两次尝试:
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)(左上角)处。
【问题讨论】:
-
是否需要在代码中动态创建布局?在 XML 中使用 FrameLayout 时,我从未遇到过此类问题
-
其实我只需要在当前(不带AbsoluteLayout)的特定位置显示一个View,这个位置是在运行时计算出来的。
-
另见
view.setTranslationX()或view.offsetLeftAndRight()