2013-08-06 31 views
2

了警告:此的LinearLayout佈局或其FrameLayout裏父母可能是無用的

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

如何解決這個警告?我找不到任何解決方案。任何幫助將不勝感激。

回答

4

我不確定你想要完成的是什麼,但是你可以通過將FrameLayout內容移動到單獨的文件並使用<include>標記將其添加回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才能正常工作。

+0

是的,它works.Thanks。 – Ponting

1

看看下面的佈局文件:

<?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而不會干擾佈局。這將解決警告。

1

將ID添加到父線性佈局。

這是沒用的,因爲父線性佈局沒有ID。這意味着在線性佈局以外的代碼中添加東西是很困難的或不可能的。

2

你所得到的警告是這個:

UselessParent

摘要:檢查父佈局是否可以被刪除。

優先級:十分之二嚴重性:警告類別:性能

帶着孩子的佈局具有沒有兄弟姐妹,不是滾動視圖或 根佈局,並且不具有的背景下,可以去除和具有 其子女直接進入父母,以獲得更高效的佈局層次結構。

它可以在林特關閉。

更新:我錯過了tabhost需要framelayout工作。禁用警告或使用鞋老鼠的解決方案應該仍然可以解決問題。 (忽略下面的段落)

假設你不需要的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" > 
+0

這是一個很好的答案。它清楚地解釋了代碼中警告和問題的原因,並提供了一個很好的答案(假設你不需要framelayout,這看起來就是你的代碼)。 – 2013-08-15 11:03:57

+0

@YekhezkelYovel,@Moberg:雖然這個答案在理論上是合理的,但你應該知道'TabHost'必須包含一個'FrameLayout'才能工作。 – ozbek

+0

@shoerat我不知道。但爲什麼使用標籤主機呢? AFAIK它不建議再使用,標籤應該使用操作欄。如果您需要支持API的舊版本,ABS將比TabHost做得更好更清潔。 – 2013-08-15 14:08:02

相關問題