Got Warning : this linearlayout layout or its framelayout parent is possibly useless

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 13:26:30

问题


<TabHost
    android:id="@+id/tabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

 // ---------------------------  Here I got warning  -------------------------
            <LinearLayout
                android:id="@+id/chat_list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:visibility="visible" >
 // --------------------------------------------------------------------------
                <ListView
                    android:id="@+id/listView"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:background="@color/white" >
                </ListView>

                <LinearLayout
                    android:id="@+id/text_entry"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="bottom"
                    android:orientation="horizontal" >

                    <EditText
                        android:id="@+id/txt_inputText"
                        android:layout_width="125dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.8"
                        android:hint="@string/enter_text"
                        android:inputType="text" />

                    <Button
                        android:id="@+id/btn_Send"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.2"
                        android:text="@string/send" />
                </LinearLayout>
            </LinearLayout>
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" >
        </TabWidget>
    </LinearLayout>
</TabHost>

How to solve this warning ? I can't find any solution. Any help will be appreciated.


回答1:


I am not sure what exactly you are trying to accomplish, but you can get rid of the warning by moving FrameLayout content to a separate file and add it back to your TabHost by using <include> tag.

A possible version could look something like this:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <include layout="@layout/tab_chat_list" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

And tab_chat_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge  xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:id="@+id/chat_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:visibility="visible" >

        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white" >
        </ListView>

        <LinearLayout
            android:id="@+id/text_entry"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/txt_inputText"
                android:layout_width="125dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.8"
                android:hint="@string/enter_text"
                android:inputType="text" />

            <Button
                android:id="@+id/btn_Send"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:text="@string/send" />
        </LinearLayout>
    </LinearLayout>
</merge>

Hope this helps.

Update

I noticed that some people are suggesting to remove FrameLayout. Although, that could have solved the problem in other cases, TabHost needs TabWidget and FrameLayout to function correctly.




回答2:


The warning you are getting is this one:

UselessParent

Summary: Checks whether a parent layout can be removed.

Priority: 2 / 10 Severity: Warning Category: Performance

A layout with children that has no siblings, is not a scrollview or a root layout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy.

It can be turned off in Lint.

Update: I missed that tabhost requires a framelayout to work. Disabling the warning or going with shoe rat's solution should still fix the problem. (ignore paragraph below)

Assuming you don't need the FrameLayout (i.e. to add stuff dynamically), you can try removing it and adjusting the LinearLayout accordingly:

<LinearLayout
    ndroid:id="@+id/chat_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:visibility="visible" >



回答3:


Take a look at the following layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout 
        android:id="@+id/rlInner"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some Text" />

    </RelativeLayout>

</LinearLayout>

There is nothing wrong with it and any activity using this layout will run without a problem. But eclipse will give you a warning:

This RelativeLayout layout or its LinearLayout parent is useless

As you can see, I only need one of the two container layouts above. Adding the RelativeLayout with id="rlInner" does nothing to change the layout. When eclipse finds a scenario like this, it gives you an appropriate warning to indicate the redundancy.

In your scenario, you can lose the FrameLayout altogether without disturbing the layout. And this would fix the warning.




回答4:


Add an ID to the parent linear layout.

It's useless because the parent linear layout doesn't have an id. It means it would be difficult or impossible to add stuff in code outside the linear layout where you got the error.



来源:https://stackoverflow.com/questions/18080316/got-warning-this-linearlayout-layout-or-its-framelayout-parent-is-possibly-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!