Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

android - WebView height = wrap_content with changing font size doesn't work

I have webview with layout_height = "wrap_content". If I increase default font size, then webview height increases too. But if I decrease default font size, then webview height doesn't decrease. So there is empty space on the bottom remaining.

I've tried following trick:

articleContent.getSettings().setDefaultFontSize(fontSize);
RelativeLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, subtitleTextView.getId());
articleContent.setLayoutParams(layoutParams);

But it didn't help. I know there is the way to fix it by recreating WebView with a code when I changing default font size, but in my situation I can not do like that. I need to keep webview, because there are some views bellow it, and I can not recreate them too.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The only solution I have found that actually works, is in the com.android.email client that comes with the Android Open Source Project. There, they have a RigidWebView which works with the wrap_content and will resize accordingly when the content changes. Source code for this can be found here:

https://android.googlesource.com/platform/packages/apps/Email/+/c19c1226da4a32e49e81c202478384f0348bcfef/src/com/android/email/view/RigidWebView.java

You'll need the Clock and Throttle classes that come with it, but with minor changes to the code, you can make it so it works in your app. Then anywhere where you would normally define a WebView, you can use RigidWebView instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...