机器人的getHeight()/的getWidth与RelativeLayout的 [英] android getHeight()/getWidth with RelativeLayout
问题描述
您好我是新来的Android和现在面临的问题:我如何才能在高度/宽度的视图,这是摆在一个相对布局?我试过的getHeight()和的getWidth()和都返回0。
Hi I'm new to Android and am facing the problem: how do I get the Height/Width of a view, which is placed in a relative layout? I tried getHeight() and getWidth() and both return 0.
我需要知道这种看法是否达到了布局的结束,这样我可以下一个视图放置在其右侧或下方。
I need to know whether this view has reached the end of the Layout so that I can place the next view to its right or below it.
或者有更好的办法做到这一点不知道当前视图的位置?
or maybe there is a better way to do this without knowing the position of the current view?
编辑: 我用这个code创建一个EditText,并将其添加到布局:
edit: I use this code to create an EditText and add it to the layout:
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。
推荐答案
正在调用的getHeight()/的getWidth()视图可以被解雇之前出这样它返回0。如果你在生命周期后调用它它会停止这个问题。
You are calling getHeight()/getWidth() before the view can be laid out so it is returning 0. If you call it later in the lifecycle it will stop this issue.
这篇关于机器人的getHeight()/的getWidth与RelativeLayout的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!