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

后端 未结 7 1594
醉话见心
醉话见心 2020-12-19 06:54

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 h

相关标签:
7条回答
  • 2020-12-19 07:01

    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.

    0 讨论(0)
  • 2020-12-19 07:01

    Just add this

    LinearLayout.LayoutParams wv_l =  new 
    
        LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    
                                    wv.setLayoutParams(wv_l);
    
    0 讨论(0)
  • 2020-12-19 07:08

    Try this,

    webview.setVisibility(View.GONE);
    webview.setVisibility(View.VISIBLE);
    
    0 讨论(0)
  • 2020-12-19 07:09

    try this

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical" >
    <WebView 
        android:id="@+id/webview1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />
    
     </LinearLayout>
    
    0 讨论(0)
  • 2020-12-19 07:16

    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.

    0 讨论(0)
  • 2020-12-19 07:24

    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.

    0 讨论(0)
提交回复
热议问题