嗨,我是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.