简体   繁体   中英

why my html file doesnt fit different android screen sizes?

I have following code,

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView mWebView = null;
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("file:///android_asset/FE/index.html");
}

but, the html page doesnt scale itself according to the screen size.

XML code ...

<?xml version="1.0" encoding="utf-8"?>
<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="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

Java Code ...

WebView mWebView = null;

mWebView = (WebView) findViewById(R.id.webView1);

mWebView.getSettings().setJavaScriptEnabled(true);

mWebView.loadUrl("file:///android_asset/index.html");

Html Code .... (index.html) put this in assets folder

<HEAD>
    <TITLE> HTML PAGE </TITLE>
</HEAD>

<BODY>
    <b> Hello World ...! </b>
</BODY>

It works perfectly for me ...

Add these Lines for your webview :

     mWebView.setInitialScale(1);
     mWebView.getSettings().setLoadWithOverviewMode(true);
     mWebView.getSettings().setUseWideViewPort(true);
     mWebView.getSettings().setSupportMultipleWindows(true);

and dont hard-code WebView layout parameters in layout.xml file. Use fill_parent for layout_width and layout_height instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM