如何使html在android浏览器中的webview上填充全屏高度

ubbxdtey  于 2023-11-15  发布在  Android
关注(0)|答案(3)|浏览(171)

我在AppCompatActivity上添加了一个webview,想让它全屏显示。我已经尝试了这个链接上提到的方法(How to make full screen in Android 4.0),但没有帮助。
下面是布局xml文件。

<?xml version="1.0" encoding="utf-8"?>
<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="com.lenovo.mds.mbgcompass.MainActivity">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView"
    />
</RelativeLayout>

字符串
下面是MainActivity类:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    webView = (WebView) findViewById(R.id.webView);
    setupWebView();
}

private void setupWebView(){
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(false);
    webView.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);
    webView.getSettings().setDefaultTextEncodingName("UTF-8");
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.loadUrl("file:///android_asset/index.html");
}


下面是html css样式,使html dom为100%的高度。但它不适用于android浏览器。如果我将高度更改为“100 hv”,它只适用于Chrome浏览器而不是android浏览器。我还应该尝试什么使html dom填充100%的屏幕高度?

var style = {
width: '100%',
margin: '0 auto',
height: '100%'
}


请看下面的屏幕截图。我认为webview正在填充设备的全屏,但webview中的html没有填充。我如何填充底部的while空间?
x1c 0d1x的数据

cedebl8k

cedebl8k1#

这是没有问题的。然而,编辑你的xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<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="com.lenovo.mds.mbgcompass.MainActivity">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:id="@+id/webView"
    />
</RelativeLayout>

字符串

svgewumm

svgewumm2#

你应该把webview的宽度和高度改为wrap_content。这对我很有效。

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

    <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

字符串

ztyzrc3y

ztyzrc3y3#

设置height: 100vh对我有帮助(height: 100%不工作,虽然)

相关问题