gpt4 book ai didi

android getHeight()/getWidth 与 RelativeLayout

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:52 25 4
gpt4 key购买 nike

您好,我是 Android 的新手,我正面临这个问题:如何获取放置在相对布局中的 View 的高度/宽度?我尝试了 getHeight() 和 getWidth() 并且都返回了 0。

我需要知道这个 View 是否已经到达布局的末尾,以便我可以将下一个 View 放在它的右边或下面。

或者在不知道当前 View 位置的情况下是否有更好的方法?

编辑:我使用此代码创建一个 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 之前调用 getHeight()/getWidth(),因此它返回 0。如果您稍后在生命周期中调用它,它将停止此问题。

关于android getHeight()/getWidth 与 RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7584957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com