Android: placing the view in any place

I am trying to place the view in an arbitrary place.

My goal: To overlay some rectangle of JPG / PNG, given coordinates related to JPG / PNG, with some other kind, say, with a gallery or with some video.

I do not want to use AbsoluteLayout as it is deprived.

So I use RelativeLayout, defining a dummy text block as a placeholder, and putting my RIGHT_TO and BELOW view in the text box.

+--------+
|TextView|
|        | (x,y)
+--------+-----------------------+
         |                       |
         |     My View           |
         |                       |
         +-----------------------+

My question is: Is there a more reliable and elegant way to do this?

, : . - . , .

  • , . :
    • init, , . . . : (
    • , (: AVD, ImageView, CTRL + F12, . , , ImageView , ).
    • .

, .

Thanks
    M.
+3
2

, , :

FrameLayout ( RelativeLayout, ). FrameLayout. , .

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 );

}

.. . :

  • // onSizeChanged.
  • onSizeChanged . , . Handler .

, -.

Meymann

+2

, , , , RelativeLayout. , , , , , , - .

AbsoluteLayout, , , , - , . , , .

+2

Source: https://habr.com/ru/post/1751200/


All Articles