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

时间:2014-11-03 07:34:35

标签: android xml 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

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

<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

4 个答案:

答案 0 :(得分:10)

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

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

  1. Calendar添加到WebView(感谢@StefanDeitmar),并通过将package all.is.well; import java.io.IOException; import java.io.RandomAccessFile; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import junit.framework.TestCase; /** * @author Naresh Bhabat * Following implementation helps to deal with extra large files in java. This program is tested for dealing with 2GB input file. There are some points where extra logic can be added in future. Pleasenote: if we want to deal with binary input file, then instead of reading line,we need to read bytes from read file object. It uses random access file,which is almost like streaming API. * **************************************** Notes regarding executor framework and its readings. Please note :ExecutorService executor = Executors.newFixedThreadPool(10); * for 10 threads:Total time required for reading and writing the text in * :seconds 349.317 * * For 100:Total time required for reading the text and writing : seconds 464.042 * * For 1000 : Total time required for reading and writing text :466.538 * For 10000 Total time required for reading and writing in seconds 479.701 * * */ public class DealWithHugeRecordsinFile extends TestCase { static final String FILEPATH = "C:\\springbatch\\bigfile1.txt.txt"; static final String FILEPATH_WRITE = "C:\\springbatch\\writinghere.txt"; static volatile RandomAccessFile fileToWrite; static volatile RandomAccessFile file; static volatile String fileContentsIter; static volatile int position = 0; public static void main(String[] args) throws IOException, InterruptedException { long currentTimeMillis = System.currentTimeMillis(); try { fileToWrite = new RandomAccessFile(FILEPATH_WRITE, "rw");//for random write,independent of thread obstacles file = new RandomAccessFile(FILEPATH, "r");//for random read,independent of thread obstacles seriouslyReadProcessAndWriteAsynch(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Thread currentThread = Thread.currentThread(); System.out.println(currentThread.getName()); long currentTimeMillis2 = System.currentTimeMillis(); double time_seconds = (currentTimeMillis2 - currentTimeMillis) / 1000.0; System.out.println("Total time required for reading the text in seconds " + time_seconds); } /** * @throws IOException * Something asynchronously serious */ public static void seriouslyReadProcessAndWriteAsynch() throws IOException { ExecutorService executor = Executors.newFixedThreadPool(10);//pls see for explanation in comments section of the class while (true) { String readLine = file.readLine(); if (readLine == null) { break; } Runnable genuineWorker = new Runnable() { @Override public void run() { // do hard processing here in this thread,i have consumed // some time and eat some exception in write method. writeToFile(FILEPATH_WRITE, readLine); // System.out.println(" :" + // Thread.currentThread().getName()); } }; executor.execute(genuineWorker); } executor.shutdown(); while (!executor.isTerminated()) { } System.out.println("Finished all threads"); file.close(); fileToWrite.close(); } /** * @param filePath * @param data * @param position */ private static void writeToFile(String filePath, String data) { try { // fileToWrite.seek(position); data = "\n" + data; if (!data.contains("Randomization")) { return; } System.out.println("Let us do something time consuming to make this thread busy"+(position++) + " :" + data); System.out.println("Lets consume through this loop"); int i=1000; while(i>0){ i--; } fileToWrite.write(data.getBytes()); throw new Exception(); } catch (Exception exception) { System.out.println("exception was thrown but still we are able to proceeed further" + " \n This can be used for marking failure of the records"); //exception.printStackTrace(); } } }添加到最外面的布局元素来使工具命名空间为人所知。
  2. 在您的代码中,将WebViewClient添加到WebView并实现tools:ignore="WebViewLayout"回调以在包装布局元素上调用xmlns:tools="http://schemas.android.com/tools"
  3. OnPageFinished()

答案 1 :(得分:8)

问题主要是因为在父 LinearLayout 中,您提供了layout_widthlayout_height作为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" >

答案 2 :(得分:6)

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

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

Eclipse本质上是创建一个“保姆状态” - 这不是我们通常这样做的方式,所以它是错误的。请原谅,但我的意见不同。

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

答案 3 :(得分:0)

之前我有同样的问题,我必须将高度设置为250dp,但我不能,所以我所做的只是将它包装到FrameLayout并设置自定义高度,你可以设置高度你想要包装器(只是没有wrap_content),它可以正常工作:

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

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