错误:Android XML:放置a<;网络视图>;在使用换行符的父元素中,内容大小可能会导致细微的错误;使用匹配父项

错误:Android XML:放置a<;网络视图>;在使用换行符的父元素中,内容大小可能会导致细微的错误;使用匹配父项,android,xml,android-linearlayout,Android,Xml,Android Linearlayout,我是Android的初学者,正在构建一个线性布局,布局XML文件中出现了这样的错误 错误 Placing a <WebView> in a parent element that uses a wrap_content size can lead to subtle bugs; use match_parent 将a放置在使用wrap_内容大小的父元素中可能会导致微妙的错误;使用匹配父项 错误显示在这部分代码中 <WebView android:layo

我是Android的初学者,正在构建一个
线性布局
,布局XML文件中出现了这样的错误

错误

 Placing a <WebView> in a parent element that uses a wrap_content size can lead to subtle bugs; use match_parent
将a放置在使用wrap_内容大小的父元素中可能会导致微妙的错误;使用匹配父项
错误显示在这部分代码中

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

谁能告诉我我的代码中有什么错误,以及如何消除错误

我认为这可能是一个非常基本的问题,但我试过了,却没有弄明白

详情:


API级别:
API 19:Android 4.2.2

问题主要是因为在父线性布局中,您提供了
layout\u宽度
layout\u高度
作为
wrap\u内容
。它应该是
match\u parent

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

我的屏幕设计是由堆叠的web视图组成的,因为它们的内容来自不同的来源。我的屏幕布局“包装内容”的高度,以便用户总是看到什么信息是从各种来源返回,没有大量的空白区域。这是一个不寻常的设计,但在许多类似的应用中已经运行了4年多。是的,我知道我必须小心身高等,但我很高兴承担起这个责任

Eclipse本质上是在创建一个“保姆状态”——这不是我们通常的做法,所以它是错误的。对不起,我的意见不同


我解决这个问题的方法是关闭XML文件并彻底清理项目。这样Eclipse就忘记了它曾经“唠叨”过这个问题,一切又好起来了。

我以前也遇到过同样的问题,我必须将高度设置为250dp,但我不能,所以我所做的只是将其包装到一个框架布局中,并将自定义高度设置为它,您可以设置要包装的高度(只是不要包装内容),它将毫无问题地工作:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="250dp">

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

</FrameLayout>

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

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

  • tools:ignore=“WebViewLayout”
    添加到您的WebView(感谢@StefanDeitmar),并通过添加
    xmlns:tools=”使工具名称空间为人所知http://schemas.android.com/tools“
    到最外面的布局元素
  • 在代码中,将WebViewClient添加到WebView中,并实现
    OnPageFinished()
    回调以调用包装布局元素上的
    requestLayout()
  • mWebView.setWebViewClient(新的WebViewClient(){
    @凌驾
    公共void onPageFinished(WebView WebView,字符串url){
    super.onPageFinished(webView,url);
    msurroundLayout.requestLayout();
    }
    }
    
    在父线性布局更改中
    android:layout\u width=“match\u parent”android:layout\u height=“match\u parent”
    @turtle非常感谢兄弟帮我解决了这个问题。我将添加这个作为答案,以便其他面临相同问题的人可以从中受益。或者,您可以添加工具:ignore=“WebViewLayout”我同意这可能是Eclipse中的一个bug。我有一个现有的WebView元素,现在可以在几个版本中正常工作。但是,今天,如果我做一个[毫无意义的]对布局的更改Eclipse开始抛出此错误。进行更改以匹配上面turtle的建议不起作用,因为Eclipse不识别“匹配父项”。我必须关闭并还原文件以消除错误。还要注意的是,我可以在外部编辑器中进行相同的更改,Eclipse不介意,并且可以很好地构建,尽管“匹配父项”仍然会导致生成失败。我进行了升级,因为修复有效,但这不是错误。这是一个新的lint规则。因此,更改布局\u宽度和布局\u高度以匹配\u父级会生成错误“错误:不允许使用字符串类型(在“布局\u高度”处,值为“匹配\u父级”)回答得好;这对我来说很有效。我认为基本问题是WebView可能需要一段时间来布局(对于某些页面,高度会经常变化)。因此,这告诉封闭布局,它们应该根据刚刚测量的高度进行重新调整。当然,除非还有其他我尚未注意到的细微错误。。。