【问题标题】:Webview taking up entire screenWebview 占据整个屏幕
【发布时间】:2020-03-11 14:23:37
【问题描述】:

我正在尝试在我的 android studios 移动应用程序项目中的一个 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>

这是我加载 web 视图的方式

    @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 is taking fullscreen in android,但我无法让它工作。任何帮助表示赞赏,谢谢。

【问题讨论】:

    标签: java xml android-studio android-layout


    【解决方案1】:

    您可以在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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-11
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多