1

我面臨嵌套佈局問題並拋出一些異常。錯誤是「此LinearLayout佈局或其LinearLayout父項無用......」。我知道我可以通過這個設置忽略這個警告。LinearLayout或其LinearLayout父項無用,我可以忽略警告消息嗎?

設置: Build Path-> Configure Build Path ....在Android Lint Preferences下查找UselessParent並將其嚴重性設置爲忽略或單擊Ignore All。

但是,Eclipse圖形佈局無法顯示任何內容並顯示錯誤消息 - 「索引:0,大小0,異常詳細信息記錄在窗口>顯示視圖>錯誤日誌中」。

我怎樣才能使圖形佈局顯示的巢佈局?

這裏是我的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="@drawable/menu_bg2" 
     android:orientation="vertical" > 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/menu_item1" 
      android:src="@drawable/alarm2x" /> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/menu_item2" 
      android:src="@drawable/bank2x" /> 

     <ImageButton 
      android:id="@+id/imageButton3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/menu_item3" 
      android:src="@drawable/brief_case2x" /> 

     <ImageButton 
      android:id="@+id/imageButton4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/menu_item4" 
      android:src="@drawable/trolley2x" /> 

     <ImageButton 
      android:id="@+id/imageButton5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/menu_item5" 
      android:src="@drawable/calculator2x" /> 

    </LinearLayout> 
</LinearLayout> 

感謝所有。

+0

什麼錯誤顯示'錯誤Log'? – Jin35 2012-07-18 08:48:22

+2

這裏是什麼使用第二個佈局....... – 2012-07-18 08:49:44

+0

添加一個更多的視圖在父佈局(可能是空白或一些可用如果有的話)刪除警告。在這段代碼中,水平佈局沒有用處。 – Kamal 2012-07-18 08:54:45

回答

1

你有兩個LinearLayout,一出來他們是使用少到現在。此警告建議您刪除其中一個。像這樣:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/LinearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/menu_bg2" 
      android:orientation="vertical" > 

    <!-- your other ImageView etc.--> 
</LinearLayout> 
+0

是的,我明白了。事實上,爲什麼我用這種方式編寫的是我想要將圖像按鈕設置爲從底部移動到頂部。我不想硬編碼這個動畫。我能怎麼做?你有最好的方法來做到這一點? – KingWu 2012-07-18 09:22:21

+0

你可以用單個LinearLayout來做到這一點。很多例子都在那裏。例如[這篇文章](http://stackoverflow.com/q/4526288/593709)處理從下到上的動畫按鈕 – 2012-07-18 09:28:38

0

你可以嘗試

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/menu_bg2" 
    android:orientation="vertical" > 

但是我從來沒有在Eclipse信任圖形佈局。

編輯:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
+0

我是懶惰的男孩= P。我想將小部件拖放到佈局中。哈哈= P – KingWu 2012-07-18 09:15:20

+0

對不起。有用。但是,我真的想要嵌套佈局是「Wrap_content」。 – KingWu 2012-07-18 09:19:24

+0

嘗試在layout_height上放置wrap_content。 – rxlky 2012-07-18 09:28:02

0

您已經打開了2個linearlayout,但只有1個linearlayout關閉。

但是,第二個線性佈局沒有意義,因爲您將一個框放入一個框中,然後在第二個框中插入項目。

+0

mmm,由於結構不完整而導致關閉「」丟失。但它在那裏,我解決了這個問題。 :) – 2012-07-18 09:05:36

+0

因爲我想動畫第二個盒子。我認爲嵌套佈局可以做到這一點。所以我繼續前進。你有什麼主意嗎? – KingWu 2012-07-18 09:12:25

+0

Adil Soomro:你的意思是關閉失蹤?實際上,我的XML有兩個關閉。我怎麼了? – KingWu 2012-07-18 09:14:11

3

因爲在第一個LinearLayout中,它只有一個視圖,並且它是一個LinearLayout。

這裏是示出結構:

LinearLayout 
    LinearLayout 
     ImageButton 
     ImageButton 
     ImageButton 
     ImageButton 
     ImageButton 
    // You should add another View or ViewGroup for the first LinearLayout 

的LinearLayout是ViewGroup中的一個子類,並且旨在將一些視圖(S),至少2個(CMIIW)。

因此,發出警告是因爲它認爲您的第一個LinearLayout沒有任何組或者沒有任何關係,如果您省略它。

對不起,如果我的英語解釋不好。

0

也許幫助你:

android:layout_alignWithParentIfMissing ="true" 
+3

我認爲這將是更多的OP和更多的訪問者,當你添加一些解釋你的內涵。 – reporter 2014-09-23 11:26:30

相關問題