【问题标题】:Got Warning : this linearlayout layout or its framelayout parent is possibly useless收到警告:此线性布局布局或其父框架布局可能没用
【发布时间】:2013-08-06 12:34:03
【问题描述】:

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

如何解决这个警告?我找不到任何解决方案。任何帮助将不胜感激。

【问题讨论】:

    标签: android warnings android-linearlayout android-framelayout


    【解决方案1】:

    我不确定您到底想要完成什么,但您可以通过将 FrameLayout 内容移动到单独的文件并使用 &lt;include&gt; 标签将其添加回您的 TabHost 来消除警告。

    可能的版本可能如下所示:

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

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

    希望这会有所帮助。

    更新

    我注意到有些人建议删除 FrameLayout。虽然,这在其他情况下可以解决问题,但TabHost 需要TabWidgetFrameLayout 才能正常工作。

    【讨论】:

      【解决方案2】:

      你得到的警告是这个:

      无用的父母

      摘要:检查是否可以删除父布局。

      优先级:2 / 10 严重性:警告类别:性能

      具有没有兄弟姐妹的子布局不是滚动视图或 根布局,并且没有背景,可以移除并拥有 它的孩子直接搬进了父母,以获得更平坦和更多 高效的布局层次结构。

      可以在 Lint 中关闭。

      更新:我错过了 tabhost 需要框架布局才能工作。禁用警告或使用鞋鼠的解决方案仍然可以解决问题。 (忽略下面的段落)

      假设您不需要 FrameLayout(即动态添加内容),您可以尝试删除它并相应地调整 LinearLayout:

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

      【讨论】:

      • 这是一个很好的答案。它清楚地解释了警告的原因和代码中的问题,并提供了一个很好的答案(假设您不需要框架布局,这似乎是查看您的代码的情况)。
      • @YekhezkelYovel, @Moberg:虽然这个答案在理论上是合理的,但你应该知道TabHost 必须包含FrameLayout 才能工作。
      • @shoerat 我不知道。但是,为什么要使用标签主机呢? AFAIK 不再推荐使用它,标签应该使用操作栏制作。如果您需要支持旧版本的 API,ABS 会比 TabHost 做得更好、更干净。
      • 无论如何,我相信保持 FrameLayout 就位并消除警告的一种可能方法是用滚动视图替换 LinearLayout。即使在极小的屏幕上,这也将保证所有元素的可见性。但我没有针对这个特定设置尝试过,所以我还不想发布答案。
      【解决方案3】:

      看看下面的布局文件:

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

      它没有任何问题,使用此布局的任何活动都将毫无问题地运行。但是eclipse会给你一个警告:

      这个RelativeLayout布局或者它的LinearLayout父级是没用的

      如您所见,我只需要上面两种容器布局中的一种。添加 id="rlInner" 的 RelativeLayout 不会改变布局。当 eclipse 发现这样的场景时,它会给你一个适当的警告来指示冗余。

      在您的场景中,您可以完全失去 FrameLayout 而不会影响布局。这将修复警告。

      【讨论】:

        【解决方案4】:

        为父线性布局添加一个 ID。

        它没用,因为父线性布局没有 id。这意味着很难或不可能在出现错误的线性布局之外的代码中添加内容。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-16
          • 2014-04-30
          相关资源
          最近更新 更多