Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid how is View position controlled?
    text
    copied!<p>According to the docs for the View class:</p> <blockquote> <p>The geometry of a view is that of a rectangle. A view has a location, expressed as a pair of left and top coordinates, and two dimensions, expressed as a width and a height. The unit for location and dimensions is the pixel.</p> <p>It is possible to retrieve the location of a view by invoking the methods getLeft() and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view.</p> <p>In addition, several convenience methods are offered to avoid unnecessary computations, namely getRight() and getBottom(). These methods return the coordinates of the right and bottom edges of the rectangle representing the view. For instance, calling getRight() is similar to the following computation: getLeft() + getWidth().</p> </blockquote> <p>My interpretation of the above is that the View's position is controlled by its "Left" and "Top" values, while its width and height are controlled by its "Width" and "Height" values. This seems especially clear considering that last sentence, where "Right" is <em>derived</em> by adding Left and Width.</p> <p>Despite this, when I use setLeft() and/or setTop() to change the position of the View, the SIZE of the View changes on screen! Meanwhile, the lower right corner of the View stays anchored to its original spot. This behavior implies that "Right" and "Bottom" are actual values, not derived as described in the docs.</p> <p>So what is really going on here? The docs say one thing, but the behavior says the opposite. What is the proper way to reposition a View?</p> <p>EDIT: I added a RelativeLayout:</p> <pre><code>myParams = new RelativeLayout.LayoutParams(300,300); myParams.addRule(RelativeLayout.CENTER_HORIZONTAL); myParams.addRule(RelativeLayout.CENTER_VERTICAL); myView.setLayoutParams(myParams); </code></pre> <p>...to create a View 300x300 centered on the screen. Works perfectly. But examining that RelativeLayout, the location seems to be controlled by leftMargin and topMargin - yet both are zero! That raises the questions of 1) how can you examine the LayoutParams to know where the View is right now, and 2) how can you alter the LayoutParams to move it to a different location?</p> <p>EDIT: As an experiment, I added an onTouch method to the View and did this within it (excerpt):</p> <pre><code>if (MotionEvent.ACTION_MOVE == iAction) { myParams = (RelativeLayout.LayoutParams) v.getLayoutParams(); myParams.leftMargin = 0; myParams.topMargin = 0; v.setLayoutParams(myParams); } </code></pre> <p>...on the theory that my vertically and horizontally centered View would then move to the upper left corner of the screen. Result: It didn't move at all. Not exactly surprising, since .leftMargin and .topMargin were already zero, but I wanted to try it just in case there was some magic hiding here.</p> <p>Other suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload