错误:Android-XML:将 放置在使用 wrap_content 大小的父元素中可能会导致微妙的错误;使用match_parent

2024-05-06

我是 Android 初学者,正在构建一个linear layout并在布局 XML 文件中收到如下错误,

Error

 Placing a <WebView> in a parent element that uses a wrap_content size can lead to subtle bugs; use match_parent

错误显示在这部分代码中

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

这是我的 XML 文件的完整代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="100" >

        <EditText
            android:id="@+id/url"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="70"
            android:ems="10" >
        </EditText>

        <Button
            android:id="@+id/go"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="30"
            android:text="GO" />
    </LinearLayout>

    <LinearLayout
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
         android:weightSum="8"

        >
    <Button
        android:id="@+id/backpage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Go Back" />

    <Button
        android:id="@+id/forwardpage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Forward Page" />

    <Button
        android:id="@+id/Refreshpage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Refresh Page" />

    <Button
        android:id="@+id/clearhistory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Clear History" />

</LinearLayout>

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


</LinearLayout>

谁能告诉我我的代码有什么问题以及如何消除该错误?

我认为这可能是一个非常基本的问题,但我尝试过但无法弄清楚。

Details:

API 级别:API 19: Android 4.2.2


与大多数答案所暗示的相反,这不是 Eclipse 中的错误。 Android Studio,但这是一个合理的警告。它是通过对布局进行 LINT 检查生成的。

您可以删除警告并解决“细微错误”,如下所示:

  1. Add tools:ignore="WebViewLayout"到你的WebView(感谢@StefanDeitmar)并通过添加使工具名称空间已知xmlns:tools="http://schemas.android.com/tools"到最外面的布局元素。
  2. 在您的代码中,将 WebViewClient 添加到您的 WebView 并实现OnPageFinished()回调来调用requestLayout()在您的包装布局元素上。
mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView webView, String url) {
        super.onPageFinished(webView, url);
        mSurroundingLayout.requestLayout();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误:Android-XML:将 放置在使用 wrap_content 大小的父元素中可能会导致微妙的错误;使用match_parent 的相关文章

随机推荐