2012-03-14 59 views
1

我有一個佈局,我希望組件填充屏幕的其餘部分(使用權重)。 Listview完美工作。佈局中嵌套的權重

在最後的2個按鈕的工作方式,我打算他們也工作,但我得到一個警告說:「嵌套的權重是壞的表現」。

我認爲這是一個問題,因爲我已經使用了Listview的權重參數,因爲當我刪除listview警告消失了。

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

     <ListView 
      android:id="@+id/listMessages_messages" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_marginBottom="5dip" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_weight="1" 
      android:background="#000000" > 
     </ListView> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <Button 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="50" 
       android:visibility="invisible" /> 

      <Button 
       android:id="@+id/btnNewConversation_message" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="5dip" 
       android:layout_weight="50" 
       android:onClick="newConversation" 
       android:text="@string/NewConversation" /> 
     </LinearLayout> 

    </LinearLayout> 
+0

所以你現在想要什麼.. – 2012-03-14 10:44:58

+0

我想知道我做錯了什麼。又如,導致警告的是什麼。因爲我想在我的應用程序 – 2012-03-14 10:51:42

回答

1

檢查這碼。我不是在收到警告

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="10"> 
    <ListView android:id="@+id/listMessages_messages" 
     android:layout_width="fill_parent" android:layout_height="0px" 
     android:layout_marginBottom="5dip" android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" android:layout_marginTop="10dip" 
     android:layout_weight="8" android:background="#000000"> 
    </ListView> 
    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="0px" android:orientation="horizontal" 
     android:weightSum="10" android:layout_weight="2"> 
     <Button android:layout_width="0px" 
      android:layout_height="wrap_content" android:layout_weight="5" 
      /> 
     <Button android:id="@+id/btnNewConversation_message" 
      android:layout_width="0px" android:layout_height="wrap_content" 
      android:layout_marginRight="5dip" android:layout_weight="5" 
      android:onClick="newConversation" /> 
    </LinearLayout> 
</LinearLayout> 

當你把兩種觀點 1.List查看 2.Linear佈局(兩個按鈕) 你需要給重屬性正確併爲按鈕水平divison調整它們也給一些權重總和屬性到第二個線性佈局使用權重均分。注意第二個線性佈局內我用寬度爲按鈕0px

+0

感謝您的回答,我無法弄清楚您在之前的回覆中對權重的含義。現在我可以開始處理我的應用程序了。 – 2012-03-14 11:46:43

+1

如果您沒有收到有關該代碼的警告,那麼您的IDE配置錯誤。嵌套權重恰好是嵌套的,只需添加weightSum來解決它們並不會改變這一事實。此外,我不明白爲什麼你應該使用fill_parent時使用所有的這些0px寬度/高度值,那有什麼推理? – 2012-03-31 17:24:24

+0

@Justin Buser - 你能提供一個嵌套的權重的簡單工作示例,它們不適合我,而且你聽起來像你把它們弄糟了嗎?謝謝。 – samosaris 2013-05-23 16:41:19

0

如果我理解你的問題正確,以下是溶膠: (比如一些10) 與頂面佈置給予一定的權重的ListView像5或6 確保佈局height屬性設置爲0px 創建子線性佈局就給剩餘重量(即列表視圖的10-重量) 作爲layout_weight兒童的佈局兒童佈局layout_height是0像素 在此佈局地方兩個按鈕。希望這是你所需要的

+0

的多個位置使用這個方法,這與解決方案非常接近,但並不完全。在我看來,列表視圖和按鈕之間不應該有重量關係。列表視圖的重量是讓它垂直填充屏幕的其餘部分(高度),並且按鈕都必須水平填充屏幕的一半(寬度)。 – 2012-03-14 11:05:37

+0

換句話說,Listview的權重只適用於外部線性佈局,而按鈕的權重適用於它們所在的線性佈局,並且不需要與列表視圖進行任何交互。 – 2012-03-14 11:07:01

+4

你真的明白他想說什麼嗎?我已經閱讀了五次,無法做出正面或反面的評論。 – 2012-03-31 17:14:32