In MainActivity onCreate() I instantiated a new View and added it via layout.addView to the activity. If i try getX() or getY() for that view I always get 0.0.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout main = (RelativeLayout)findViewById(R.id.main);
RelativeLayout.LayoutParams squarePosition = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
GameToken square1 = new GameToken(this,GameToken.SQUARE, fieldHeight,fieldWidth);
squarePosition.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
squarePosition.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
main.addView(square1, squarePosition);
System.out.println(square1.getX()); //Prints '0.0'
System.out.println(square1.getY()); //Prints '0.0'
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…