Android 如何制作全屏网页视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20179802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:25:17  来源:igfitidea点击:

How to make a full screen webview

androidwebviewfullscreen

提问by Arihant

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.

如何使android应用程序的webview全屏。我在布局 xml 文件中添加了 webview,但它不会延伸到布局的边缘,webview 的所有边都有某种类型的边距。我还添加了一张图片,让你们了解它的实际外观。

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

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

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

我说的是 webview 周围的空间,而不是通知栏或标题栏

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>

回答by VishalKale

From above code remove padding tags-

从上面的代码中删除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.

它将完成这项工作,并确保删除您正在呈现到 WebView 中的 HTML 的边距/填充,其中可能包含那些留下一些空间的标签。

回答by Joel Fernandes

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

只需android:theme在 AndroidManifest.xml 文件中添加(或更改)活动的属性

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

回答by Vijaysachin

Android Version: 4.2.2 - Device: Vega Tablet

Android 版本:4.2.2 - 设备:Vega 平板电脑

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

WebView 隐藏状态和系统栏并显示完整的屏幕我遵循了这些步骤。

AndroidManifest.xml

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

主活动.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.

上述过程对我有用,我的应用程序显示在完整屏幕中。

回答by Marko Krustev

Put webview in to dialog popup window.

将 webview 放入对话框弹出窗口。

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();

回答by HarlemSquirrel

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

从文档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)

FILL_PARENT(在 API Level 8 及更高版本中重命名为 MATCH_PARENT),这意味着视图希望与其父视图一样大(减去填充)

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

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

回答by Sushant Gautam

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

您应该更改相对布局设置。并删除填充设置,如果在 XML 文件上的 webview 上有任何设置。

<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">

回答by Mohit Verma

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

您可以在 res/values 文件夹中的 dimens.xml 中设置尺寸。默认为 16dp。所以可以相应地设置它。

enter image description here

enter image description here