获取RelativeLayout尺寸
如何获取特定布局(如线性布局或相对布局)的测量值,并将其layout_width和layout_height设置为wrap_content?使用此方法
layout.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
layout.layout(0, 0,
layout.getMeasuredWidth(),
layout.getMeasuredHeight());
返回 null。我正在尝试从相对布局中创建位图图像,有点像屏幕截图。
How do I get the measurements of a certain layout like linearlayout or relativelayout with its layout_width and layout_height set to wrap_content? using this method
layout.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
layout.layout(0, 0,
layout.getMeasuredWidth(),
layout.getMeasuredHeight());
returns null. I'm trying to create a bitmap image out of the relativelayout, kinda like a screenshot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要在 onCreate() 方法上执行此操作。试试这个:
当大小已知时,在 onCreate() 之后调用此方法。
Don't do it on the onCreate() method. Try this:
This method is called after onCreate(), when the sizes are already known.
您可以在视图上使用 getMeasuredWidth() 和 getMeasuredHeight() 。
请记住,必须首先布局视图。你只是不能在 onCreate 中执行此操作,因为尺寸尚不清楚。你必须等到它已经布局。
You can use getMeasuredWidth() and getMeasuredHeight() on your view.
Remember the view has to have been layout first. You just CANNOT do it in onCreate, because the sizes are not known yet. You have to wait until it has been layout.
公共类 AndroidResizeView 扩展 Activity {
}
public class AndroidResizeView extends Activity {
}