android - How to set the four parameters of View.layout(l, t, r, b) -


i facing same problem in this question, have difficulty in understanding exact meaning of 4 position parameters in layout(int l, int t, int r, int b). know represent left, top, right , bottom respectively relative parent, "relative" exactly?

for example, if translate button down 100 pixel, in order move clickable area down 100 pixel should set

//            l   t   r   b button.layout(0, 100, 0, button.getheight()+100) 

? integer 100 of second parameter mean 100 pixel down relative top of parent? fourth parameter means button.getheight()+100 relative top of parent also??? tested on device, clickable area not move down want. i'm confused here, appreciated.

the parameters layout relative parent view's upper left corner. x increases move right , y increases move down. not relative view's current position.

you don't want call layout on child view outside of parent's onlayout method. there several other bits of internal bookkeeping happen result of calling layout change internal state in way don't want simple animations.

typically layout called this:

child.layout(x, y, x + child.getmeasuredwidth(), y + child.getmeasuredheight()); 

the parameters absolute, (0, 0) @ parent's upper left corner.

but if you're moving view around animation want more like:

// adjust horizontally; dx = change in x position child.offsetleftandright(dx);  // adjust vertically; dy = change in y position child.offsettopandbottom(dy); 

the parameters offset* methods are relative view's current position.

if write viewgroup class moves child views around in way, remember can layout request @ time. @ point viewgroup's onlayout method try position child based on usual layout rules , need adjust positioning reflect other accumulated movement want preserve.


Comments