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

Cod*_*eek 10 xml android android-linearlayout

我是Android的初学者,linear layout在这样的布局XML文件中构建并获得错误,

错误

 Placing a <WebView> in a parent element that uses a wrap_content size can lead to subtle bugs; use match_parent
Run Code Online (Sandbox Code Playgroud)

错误显示在代码的这一部分中

<WebView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/browse"
        />
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我我的代码有什么问题,我怎样才能摆脱错误?

我认为这可能是一个非常基本的问题,但我试过并且无法弄明白.

细节:

API级别: API 19: Android 4.2.2

Rid*_*lly 10

与大多数答案所暗示的相反,这不是Eclipse中的错误.Android Studio,但是公平警告.它是通过LINT检查你的布局产生的.

您可以删除警告并解决'subtile bugs',如下所示:

  1. 添加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();
          }
      }
   );
Run Code Online (Sandbox Code Playgroud)


tur*_*tle 8

问题主要是因为在父LinearLayout中,您提供了layout_widthlayout_heightas wrap_content.它应该是match_parent.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)


Col*_*lin 6

实际上我认为这是Eclipse中的一个错误,或者是最新版本的20 Android SDK.

我的屏幕设计由堆叠的Web视图组成,因为它们的内容来自不同的来源.我的屏幕布局为高度"包装内容",以便用户始终可以看到从各种来源返回的信息,而没有大量的空白区域.它是一个不寻常的设计,但在许多类似的应用程序上已经运行了4年多.是的,我知道我必须小心高度等,但我很乐意接受这个责任.

Eclipse本质上是创建一个"保姆状态" - 这不是我们通常这样做的方式,所以它错了.对不起,但我的意见不同.

我解决这个问题的方法是关闭 XML文件并完全清理项目.这样Eclipse就忘记了这个问题曾经"保姆唠叨",一切都很好.

  • 或者,你可以添加工具:ignore ="WebViewLayout"到你的WebView和Android Lint会停止唠叨你. (4认同)
  • 我投票是因为修复有效,但这不是错误。这是一个新的 lint 规则。 (2认同)