gpt4 book ai didi

android - 错误 : Android-XML:Placing a in a parent element that uses a wrap_content size can lead to subtle bugs; use match_parent

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:31 26 4
gpt4 key购买 nike

我是 Android 初学者,正在构建一个线性布局,但在这样的布局 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

最佳答案

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

您可以像这样删除警告并解决“微妙的错误”:

  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();
}
}

关于android - 错误 : Android-XML:Placing a <WebView> in a parent element that uses a wrap_content size can lead to subtle bugs; use match_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com