How to make a full screen webview

2019-01-23 04:52发布

问题:

How to make the webview of an android application full screen. I have added the webview on a layout xml file but it doesn't stretches out till the edges of the layout, there is some type of margin along all the side of the webview. I am also adding an image to give you guys a hint on what actually it is looking like.

What i am talking about is the space all around the webview, not the notification bar or the title bar

Here's the layout code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".WebView" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Shake / Tilt Your Phone To Get Accelerometer Motion Alerts" />

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

回答1:

From above code remove padding tags -

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

It will do the job and also be sure to remove the HTML's margins/padding which you are rendering into the WebView that might contain those tags which leaves some space.



回答2:

Just add (or change) the activity's android:theme attribute with following line in AndroidManifest.xml file

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"


回答3:

Android Version: 4.2.2 - Device: Vega Tablet

WebView to Hide Status and System Bar and displaying complete screen I followed these steps.

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

MainActivity.java

super.onCreate(savedInstanceState);
getWindow().getDecorView().setSystemUiVisibility(0x10);
setContentView(R.layout.activity_main);
String url = "http://edition.cnn.com/";
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUserAgentString("Desktop");
webView.loadUrl(url);

The above process worked for me and my app is displayed in complete screen.



回答4:

Put webview in to dialog popup window.

WebView webview = new WebView(this/*put your activity object*/);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(false);
    webview.getSettings().setSupportZoom(false);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setBackgroundColor(Color.TRANSPARENT);
    webview.loadUrl("http://www.awwwards.com/websites/responsive-design/");

    RelativeLayout.LayoutParams paramsWebView = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    Dialog dialog = new Dialog(this/*put your activity object*/, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    dialog.addContentView(webview, paramsWebView);
    dialog.show();


回答5:

You can set dimensions in dimens.xml found in res/values folder. By default it is 16dp. So can set it accordingly.



回答6:

You should change the relative layout settings. And remove padding settings, if any on webview on XML file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/frame">



回答7:

From the docs https://developer.android.com/guide/webapps/webview.html

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

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)

https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html