更新时间:2023-11-19 14:19:22
解决方案是pretty丑陋的,它不是相对的,但有些人可能说这是合理的:
添加的FrameLayout作为容器(而非RelativeLayout的下面说明)。要显示的看法是的FrameLayout的孩子。
为了将它在的地方,添加填充。
无效addViewInAnArbitraryRect(矩形RECT,
上下文的背景下,
查看subjectView,
查看父){
集装箱的FrameLayout =新的FrameLayout(背景);
parent.addView(容器);
container.setPadding(rect.left,
rect.top,
container.getWidth() - rect.right,
container.getHeight() - rect.bottom);
container.addView(subjectView);}
注意:您可能希望将页面的坐标调整到屏幕上的坐标。只要记住:
我希望它帮助别人。
Meymann
I have been trying to place a view in an arbitrary location.
My aim: To overlay some rectangle of a JPG/PNG, given coordinates that relate to the JPG/PNG, with some other view, say, Gallery, or some video.
I don't want to use AbsoluteLayout, as it is depricated.
Therefore, I am using RelativeLayout, defining a dummy textbox as a placeholder, and putting my view RIGHT_TO and BELOW the textbox.
+--------+
|TextView|
| | (x,y)
+--------+-----------------------+
| |
| My View |
| |
+-----------------------+
My question is: Is there a more robust and elegant way to do that?
The problem with the way suggested above, is that it's very fragile: The coordinates need to be recalculated for every new screen. My view overlays something below. It needs to be accurately placed, which is pretty difficult to do.
So I would like to find a better way to do that.
Thanks
M.
The solution is pretty ugly, it's not relative, but some may say it's plausible:
Adding a FrameLayout as a container (rather than the RelativeLayout noted below). The view to display is the child of that FrameLayout. In order to place it in place, add padding.
void addViewInAnArbitraryRect( Rect rect,
Context context,
View subjectView,
View parent ) {
FrameLayout container = new FrameLayout( context );
parent.addView( container );
container.setPadding( rect.left,
rect.top,
container.getWidth() - rect.right,
container.getHeight() - rect.bottom );
container.addView( subjectView );
}
Note: You may want to adjust the coordinates of the page to the coordinates on screen. Just remember:
I hoped it helped somebody.
Meymann