【发布时间】:2011-09-28 14:37:11
【问题描述】:
您好,我是 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()); -
您能否提供更多详细信息?我是安卓新手。谢谢!
-
location[1]是 Y 坐标,我认为您需要 X 坐标 (location[0]) [developer.android.com/reference/android/view/…] -
你是对的,但 location[0] 也是 0。
标签: android position android-relativelayout