WebView height = wrap_content with changing font s

2019-01-26 13:11发布

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.

7条回答
Luminary・发光体
2楼-- · 2019-01-26 13:22

Try this,

webview.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-26 13:23

This issue should be fixed in the KitKat WebView.

For WebView versions before KitKat there aren't any good workarounds I know of. Altering the width of the WebView should force a height recalculation but it might also lead to visual glitches.

I think that temporarily forcing a height of 0 and then flipping it back to wrap_contents should get rid of the excess padding too.

查看更多
Emotional °昔
4楼-- · 2019-01-26 13:24

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.

查看更多
成全新的幸福
5楼-- · 2019-01-26 13:28

Just add this

LinearLayout.LayoutParams wv_l =  new 

    LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

                                wv.setLayoutParams(wv_l);
查看更多
虎瘦雄心在
6楼-- · 2019-01-26 13:29

The issue is referenced Android's issue tracker here:
https://code.google.com/p/android/issues/detail?id=18726

If what you really want is a WebView wrapping your content it looks like the only work-around at the moment is to create a new WebView every time your content changes (which may not be acceptable when targeting low memory devices). It is the solution I have adopted anyway.

查看更多
太酷不给撩
7楼-- · 2019-01-26 13:34

Try the following combination:

<LinearLayout
     android:id="@+id/xxxxxxxxxxxxxxxxxxx"
     android:layout_width="match_parent"
     android:layout_height="200dp"
     android:orientation="vertical" >

     <TextView
         android:id="@+id/yyyyyyyyyyyyyyyyyyyyy"
         style="@style/Etiquetas"
         android:text="@string/hola" />

     <WebView android:paddingLeft="10dp" 
         android:id="@+id/zzzzzzzzzzzzzzz"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@null" >
     </WebView>

</LinearLayout>

Java:

LinearLayout. LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
xxxxxxxxxxxxxxxxxxx.setLayoutParams(params);
WebView  zzzzzzzzzzzzzzz = (WebView) findViewById(R.id.zzzzzzzzzzzzzzz); 
zzzzzzzzzzzzzzz.setBackgroundColor(0x00000000);
zzzzzzzzzzzzzzz.loadData(Html.fromHtml(mistringsqlite).toString(), "text/html", null);
查看更多
登录 后发表回答