Android Webview rem units scale to large for boxes

EDIT: This error is Webview overriding the default minimum font size. In my example, Webview sets the minimum font size to 8px somewhere. The solution is below :)

Android Webview rem devices scale to large. All rem modules look 8 times too big and inappropriate in Android Webview. The only rem tag that works correctly is font-size: 30rem; for text. Everything else seems to scale the path to the big. those. 100rem = 800px @ 1.0px. Oddly enough, after I skip 8.0px when scaling, the window starts to grow.

This example works in every browser that supports rem modules except Webview. Does anyone have an idea?

: http://digitalknob.com/rem.html
JSFiddle: https://jsfiddle.net/aquawicket/71p49ag9/
Android: http://digitalknob.com/remTest.apk

mWebView.getSettings(). setLoadWithOverviewMode (true);
mWebView.getSettings(). SetUseWideViewPort (true);
< meta name= "viewport" content = "width = device-width, user-scalable = no" /" > .
.

rem.html

<!DOCTYPE html>
<html style="font-size:1.0px;">
<head>
</head>
<body style="font-size:16em;">

<button style="position:absolute;width:100px;height:100px;font-size:60px;" onclick="ScaleDown()">-</button>
<button style="position:absolute;left:110px;width:100px;height:100px;font-size:60px;" onclick="ScaleUp()">+</button>
<a id="text" style="position:absolute;top:20px;left:220px;font-size:60px;">1.0px</a>

<!-- FIXME: rem units appear 8 times too large and out of place in Android Webview. 
     The only rem tag that works correctly is font-size:30rem; 
     This html file works as expexted on all other browsers. -->
<div style="position:absolute;top:115px;width:100rem;height:100rem;background-color:green;">
    <a style="position:relative;top:30rem;left:16rem;font-size:30rem;">Scale</a>
</div>

<script>
var scale = window.devicePixelRatio;
Update_Scale();

function ScaleDown(){
    scale -= 0.5;
    Update_Scale();
}

function ScaleUp(){
    scale += 0.5;
    Update_Scale();
}

function Update_Scale(){
    document.documentElement.style.fontSize = parseFloat(scale).toFixed(1)+"px";
    document.getElementById("text").innerHTML = parseFloat(scale).toFixed(1)+"px";
}
</script>

</body>
</html>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="digitalknob.remTest"
      android:installLocation="auto"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:targetSdkVersion="19" android:minSdkVersion="19"></uses-sdk>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:label="remTest"
                android:icon="@mipmap/icon"
                android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <activity android:name="digitalknob.remTest.WebviewActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>
</manifest>


WebviewActivity.java

package digitalknob.remTest;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebviewActivity extends Activity {

    private WebView mWebView;

    @Override protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        mWebView=(WebView)findViewById(R.id.webview_webview);
        mWebView.setWebContentsDebuggingEnabled(true); //Debuggable in Chrome
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.loadUrl("http://digitalknob.com/rem.html");
    }

    @Override public void onBackPressed(){
        System.exit(0);
    }
}


webview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView
        android:id="@+id/webview_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</RelativeLayout>
+4
1

, - .

mWebView.getSettings().setMinimumFontSize(1);
mWebView.getSettings().setMinimumLogicalFontSize(1);

setMinimumFontSize

, /. , - Android.

+6

Source: https://habr.com/ru/post/1614866/


All Articles