How to set the four parameters of View.layout(l, t, r, b)
Asked Answered
L

1

6

I am facing the same problem in this question, but I have difficulty in understanding the exact meaning of the four position parameters in layout(int l, int t, int r, int b). I know they represent left, top, right and bottom respectively relative to parent, but "relative" to where exactly?

For example, if I translate the button down 100 pixel, in order to move the clickable area down 100 pixel should I set

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

? Does the integer 100 of second parameter mean 100 pixel down relative to the top of parent? Is the fourth parameter means button.getHeight()+100 relative to the top of parent also??? I tested on my device, the clickable area does not move down as I want. I'm very confused here, any help is greatly appreciated.

Loads answered 6/5, 2013 at 12:57 Comment(5)
Have you tried x, y, width, height already?Cahilly
what do x,y mean?Loads
x is another word for left, y is top.Cahilly
I tried, it doesn't work that way. I don't think it is the right meaning.Loads
Does button.layout(0, 0, 100, 100); show something?Cahilly
T
9

The parameters to layout are relative to the parent view's upper left corner. X increases as you move to the right and Y increases as you move down. It is not relative to the view's current position.

You probably don't want to call layout on a child view outside of its parent's onLayout method. There are several other bits of internal bookkeeping that happen as a result of calling layout that will change internal state in a way you don't want for simple animations.

Typically layout is called like this:

child.layout(x, y, x + child.getMeasuredWidth(), y + child.getMeasuredHeight());

The parameters are absolute, with (0, 0) at the parent's upper left corner.

But if you're simply moving a view around for animation you probably want to do something more like:

// Adjust horizontally; dx = change in x position
child.offsetLeftAndRight(dx);

// Adjust vertically; dy = change in y position
child.offsetTopAndBottom(dy);

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

If you write a ViewGroup class that moves its child views around in this way, remember that you can get a layout request at any time. At that point your ViewGroup's onLayout method will try to position the child based on its usual layout rules and it will need to adjust this positioning to reflect any other accumulated movement you want to preserve.

Tacy answered 6/5, 2013 at 15:26 Comment(2)
Thanks. Your answer is very clear, only that my clickable area still won't move. Is it right if I call view.layout after setting an animation to the layout layout.set(animation)? Must I do this in a custom onAnimationListener method onAnimationEnd()?Loads
The left/right/top/bottom properties of a view are what are used for touch event targeting and dispatch. As long as you're moving the view's frame your touch events will go to the right place. Using the pre-Honeycomb animation system will indeed leave you with views that draw in a different location than their "true" location in terms of where touch events are sent. The translationX/translationY properties added later do affect touch event targeting.Tacy

© 2022 - 2024 — McMap. All rights reserved.