加载Android后获取WebView内容高度
我试图根据Webview的内容高度在其上应用展开折叠功能,但我总是得到错误的值 这是我的密码加载Android后获取WebView内容高度,android,html,webview,Android,Html,Webview,我试图根据Webview的内容高度在其上应用展开折叠功能,但我总是得到错误的值 这是我的密码 public class MesuredHeightWebView extends WebView { public MesuredHeightWebView(Context context) { super(context); } public MesuredHeightWebView(Context context, AttributeSet attrs)
public class MesuredHeightWebView extends WebView {
public MesuredHeightWebView(Context context) {
super(context);
}
public MesuredHeightWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MesuredHeightWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void invalidate() {
super.invalidate();
if (getContentHeight() > 0) {
// WebView has displayed some content and is scrollable.
if(listener!=null)
listener.updateContentHeight(getContentHeight());
}
}
WebViewContentHeight listener;
public void setChangeContentListener(WebViewContentHeight listener) {
this.listener = listener;
}
}
然后在片段中,我试图得到内容的高度
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress==100)
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
webView.setChangeContentListener(new WebViewContentHeight() {
@Override
public void updateContentHeight(int height) {
if(height>0 && getActivity()!=null) {
System.out.println("the height2 is" + height + " " + Utils.convertPixelsToDp(height, getActivity()));
final int text_height = height;
但我的问题是我总是得到错误的结果
谢谢请使用以下方法代替
getContentHeight()
方法:
computeHorizontalScrollRange(); -> for width
computeVerticalScrollRange(); -> for height
这两种方法将返回整个可滚动的宽度/高度,而不是屏幕上webview的实际宽度/高度谢谢你,它的工作原理非常好,回答得很好!将它与onScrolledChanged()结合使用,可以更有效地处理顶部和底部。这些方法是针对scrollview的,而问题是针对webview的。我所处的情况是,我只需要webview高度,而不是整个卷轴view@Siavash垂直滚动范围实际上是WebView的高度。您能解释一下为什么ContentListener必须在可运行范围内吗?如果我们只设置侦听器而不设置它,我们不能得到准确的值吗?