Webview占据整个屏幕

3
我正在尝试在Android Studio移动应用程序项目的一个XML文件中实现WebView。我想做的是有一些按钮,可以浏览不同页面的本地应用程序,并在这些按钮下面放置一个网页。然而,WebView一直占据整个屏幕空间,我创建的按钮已经无法看到。以下是XML参考代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"


    android:background="#ffffff"
    tools:context=".GetInvolved" >

    <Button
        android:id="@+id/homeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="51dp"
        android:adjustViewBounds="true"
        android:background="@android:color/black"
        android:clickable="false"
        android:onClick="goHome"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="408dp"
        android:layout_height="572dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/homeButton" />

</androidx.constraintlayout.widget.ConstraintLayout>

以下是我如何加载webview的方式

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

        view = findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new WebViewClient() {
            @Override
            public void onLoadResource(WebView webView, String url) {
                try {
                    // remove the nav bar and the footer from each loaded page.
                    view.loadUrl("javascript:(window.onload = function() { " +
                            "})()");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        view.loadUrl("https://www.google.com/");
    }

我尝试过几个不同的Stack Overflow文章,比如这篇 Webview在Android中占据全屏 但是我没能让它起作用。谢谢任何帮助。

1个回答

0
你可以在 Webview 标签中添加 app:layout_constraintHorizontal_biasapp:layout_constraintVertical_bias
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
     >

    <Button
        android:id="@+id/homeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="51dp"
        android:adjustViewBounds="true"
        android:background="@android:color/black"
        android:clickable="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="408dp"
        android:layout_height="572dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/homeButton"
        app:layout_constraintVertical_bias="0.037" />

</androidx.constraintlayout.widget.ConstraintLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接