Android:如何获取自定义View的高度和宽度?

发布于 2024-09-30 01:47:47 字数 147 浏览 4 评论 0原文

如何使用 getMeasuredWidth()getMeasuredHeight()?它总是返回 0。这与 getHeight()getWidth() 有什么区别?

How do I use getMeasuredWidth() and getMeasuredHeight()? It always returns 0. What is the difference between this and getHeight() and getWidth()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

神回复 2024-10-07 01:47:47

刚刚找到了一个获取自定义视图的高度和宽度的解决方案:

@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
    super.onSizeChanged(xNew, yNew, xOld, yOld);

    viewWidth = xNew;
    viewHeight = yNew;
}

它适用于我的情况。

Just got a solution to get height and width of a custom view:

@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
    super.onSizeChanged(xNew, yNew, xOld, yOld);

    viewWidth = xNew;
    viewHeight = yNew;
}

Its working in my case.

猥︴琐丶欲为 2024-10-07 01:47:47

您可以使用以下方法获取视图的宽度和高度,
例如,

int height = yourView.getLayoutParams().height;
int width = yourView.getLayoutParams().width;

这给出了 XML 布局中指定的视图的转换值。

假设如果 XML 中指定的高度值为 53dp,则您将得到整数转换后的值为 80。

You can use the following method to get the width and height of the view,
For example,

int height = yourView.getLayoutParams().height;
int width = yourView.getLayoutParams().width;

This gives the converted value of the view which specified in the XML layout.

Say if the specified value for height is 53dp in XML, you will get the converted value in integer as 80.

输什么也不输骨气 2024-10-07 01:47:47

不要尝试将它们放入其构造函数中。尝试在 onDraw() 方法中调用它们。

Don't try to get them inside its constructor. Try Call them in onDraw() method.

双马尾 2024-10-07 01:47:47

我还迷失了 getMeasuredWidth()getMeasuredHeight() getHeight()getWidth() 很长一段时间时间......后来我发现onSizeChanged()方法真的很有帮助。

新博客文章:如何在 Android 中获取自定义视图(扩展视图)的宽度和高度尺寸 http://syedrakibalhasan.blogspot.com/2011/02/how-to-get-width-and-height-dimensions.html

I was also lost around getMeasuredWidth() and getMeasuredHeight() getHeight() and getWidth() for a long time.......... later i found onSizeChanged() method to be REALLY helpful.

New Blog Post: how to get width and height dimensions of a customView (extends View) in Android http://syedrakibalhasan.blogspot.com/2011/02/how-to-get-width-and-height-dimensions.html

极致的悲 2024-10-07 01:47:47

getHeight()getMeasuredHeight() 之间的区别在于,第一个方法将返回 View 的实际高度,第二个方法将返回汇总高度View 的子级。换句话说,getHeight() 返回视图高度,getMeasuredHeight() 返回该视图需要显示其所有元素的高度

The difference between getHeight() and getMeasuredHeight() is that first method will return actual height of the View, the second one will return summary height of View's children. In ohter words, getHeight() returns view height, getMeasuredHeight() returns height which this view needs to show all it's elements

挽手叙旧 2024-10-07 01:47:47

getHeight 获取高度,getWidth 获取宽度。但你调用这些方法太早了。

如果您在 onCreateonResume 中调用它们,则视图尚未绘制。您必须在绘制视图后调用它们。

Well getHeight gets the height, and getWidth gets the width. But you're calling those methods too soon.

If you're calling them in onCreate or onResume, the view isn't drawn yet. You have to call them after the view has been drawn.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文