安卓:View.measure(INT,INT)总想为(0,0) [英] Android: View.measure(int,int) always wants to be (0,0)

查看:160
本文介绍了安卓:View.measure(INT,INT)总想为(0,0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我已经加入到一个视图组的WebView:

  web视图=新的WebView(背景);
addView(web视图,ViewGroup.LayoutParams.WRAP_CONTENT);

本住在我的ViewGroup的构造。

然后在布局,我想这样做:

  webView.measure(View.MeasureSpec.makeMeasureSpec(的getWidth(),View.MeasureSpec.EXACTLY)
                View.MeasureSpec.makeMeasureSpec(3000,View.MeasureSpec.AT_MOST));

不幸的是,除非我指定完全中,webView.getMeasuredHeight()总是返回 0 。我想要做的,是确定的WebView有多大希望成为这样我可以躺在它周围的其他元素。我指定我想要的网页流量足够大,以涵盖它的内容,我提供了空间慷慨的金额。那么,为什么它仍然0?

感谢

更新1

到时候web视图获取措施()请求,应该知道它有多少数据了,不是吗?

  web视图=新的WebView(背景);
webView.loadData(< HTML和GT;< / HTML>中为text / html,UTF-8);
addView(web视图,新ViewGroup.LayoutParams(的getWidth(),1000));


解决方案

  addView(web视图,ViewGroup.LayoutParams.WRAP_CONTENT);

是非常错误的。第二个参数,当为一个int过去了,是父里面的视图索引。这不会做你认为它。你应该通过的新实例的LayoutParams 代替。

您的问题可能是你的的WebView之前做了测量有时间来加载HTML文档。你衡量约束 AT_MOST 3000,0绝对尊重的约束。的WebView只是告诉你,其含量为0,高度了。

I have a WebView that I've added to a view group:

webView = new WebView(context);
addView(webView, ViewGroup.LayoutParams.WRAP_CONTENT);

This lives in my viewgroup's constructor.

Then on layout, I want to do this:

webView.measure(View.MeasureSpec.makeMeasureSpec(getWidth(),View.MeasureSpec.EXACTLY),  
                View.MeasureSpec.makeMeasureSpec(3000, View.MeasureSpec.AT_MOST));

Unfortunately, unless I specify EXACTLY, the webView.getMeasuredHeight() always returns 0. What I want to do, is determine how big the webview wants to be so that I can lay other elements around it. I specified that I want the webview to be big enough to encompass it's content and I provided generous amount of space. So why is it still 0?

Thanks

Update 1

By the time webView gets measure() request, it should know how much data it has, no?

webView = new WebView(context);
webView.loadData("<html></html>","text/html", "utf-8");
addView(webView, new ViewGroup.LayoutParams(getWidth(), 1000));

解决方案

This:

addView(webView, ViewGroup.LayoutParams.WRAP_CONTENT);

is very wrong. The second parameter, when passed as an int, is the index of the view inside the parent. This does not do what you think it does. You should pass a new instance of LayoutParams instead.

Your problem is probably that you do a measurement before the WebView had time to load the HTML document. You measure with the constraint AT_MOST 3000, and 0 definitely respects that constraints. WebView is just telling you that its content has a height of 0 for now.

这篇关于安卓:View.measure(INT,INT)总想为(0,0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆