你好,我是一个Android新手,遇到了一个问题:如何获取相对布局中的视图的高度/宽度?我尝试使用getHeight()和getWidth()方法,但两者都返回0。
我需要知道这个视图是否到达了布局的末尾,以便我可以将下一个视图放置在其右侧或下方。
也许有一种更好的方法可以在不知道当前视图位置的情况下完成这个任务吗?
编辑: 我使用以下代码创建EditText并将其添加到布局中:
EditText et1 = new EditText(context);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.RIGHT_OF,curView.getId());
et1.setLayoutParams(p);
et1.setId(loopid++);
curLayout.addView(et1);
curView=et1;
然后调用:
int[] location=new int[2];
curView.getLocationOnScreen(location);
Log.d("right:", String.valueOf(location[1]));
并且它显示为0。
View.post(new Runnable());
。 - Adil Soomrolocation[1]
是Y坐标,我认为你需要X坐标 (location[0]
) [http://developer.android.com/reference/android/view/View.html#getLocationOnScreen%28int[]%29]。 - beetstra