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

后端 未结 4 1333
夕颜
夕颜 2021-02-13 13:20

I am a beginner in Android and building a linear layout and getting an error in the layout XML file like this,

Error

 Placi         


        
相关标签:
4条回答
  • 2021-02-13 13:46

    I had the same issue before, I had to set the height to 250dp, but I couldn't, so what I did was just wrap it into a FrameLayout and set the custom height to it, you can set the height you want to the wrapper (just not wrap_content) and it will work with no problem:

    <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>
    
    0 讨论(0)
  • 2021-02-13 13:50

    The issue is mainly because in the parent LinearLayout, you have provide layout_width and layout_height as wrap_content. It should be 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" >
    
    0 讨论(0)
  • 2021-02-13 14:01

    Actually I think that this is a bug in Eclipse, or the latest version 20 Android SDK.

    I have screen designs that are made up of stacked web views because their contents come from different sources. My screen layouts "wrap content" for height so that the user always sees what information is returned from the various sources without vast areas of blank. Its an unusual design, but one that has worked well for 4+ years on many similar applications. Yup, I know that I have to be careful about heights, etc, but I'm happy to take on that responsibility.

    Eclipse is essentially creating a "Nanny State" - that's not the way we normally do it so its wrong. Pardon me but I differ in opinion.

    The way I got around this was to close the XML file and completely clean the project. This way Eclipse forgets that it ever "nanny nagged" about this issue and everything is good again.

    0 讨论(0)
  • 2021-02-13 14:06

    Contrary to what most answers imply, this is not a bug in Eclipse resp. Android Studio, but a fair warning. It's produced by a LINT check on your layout.

    You can remove the warning and work around the 'subtile bugs' like so:

    1. Add tools:ignore="WebViewLayout" to your WebView (thanks to @StefanDeitmar) and make the tools namespace known by adding xmlns:tools="http://schemas.android.com/tools" to your outmost layout element.
    2. In your code, add a WebViewClient to your WebView and implement the OnPageFinished() callback to invoke a requestLayout() on your wrapping layout element.
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView webView, String url) {
            super.onPageFinished(webView, url);
            mSurroundingLayout.requestLayout();
        }
    }
    
    0 讨论(0)
提交回复
热议问题