2014-07-16 34 views
1

我有一個問題,讓我瘋狂不同的佈局相同的權重得到不同的大小

在我的佈局我有兩個不同的LinearLayouts。第一個是這個

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="8" 
      android:gravity="center" 
      android:text="@string/lblPais" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblJ" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblG" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblP" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPF" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPC" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblDif" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPuntos" /> 

    </LinearLayout> 

,第二個是這個佈局

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="15" > 

     <TextView 
      android:id="@+id/lblPais1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="8" 
      android:gravity="center" 
      android:text="@string/lblPais" /> 

     <TextView 
      android:id="@+id/lblJ1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblG1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblP1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblPF1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblPC" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblDif1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPtsVacio" /> 

     <TextView 
      android:id="@+id/lblPuntos1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPtsVacio" /> 

    </LinearLayout> 

正如你可以看到這兩種佈局得到了layout_width屬性(match_parent)相同的值。在兩個佈局中,我有相同數量的元素(8個TextView)。除第一個TextView外,每個TextView具有相同的權重,權重爲8.

如果兩個佈局相同(只是不同TextView上的標籤),爲什麼它們具有不同的大小?如果你想所有的佈局是一樣的weight

orientation是垂直的,改變我無法理解它,它的駕駛我瘋了...

+1

üHV給出的第二佈局的機器人:weightSum = 15 – KOTIOS

回答

2

試試這個辦法,希望這將幫助你解決你的問題。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="8" 
      android:gravity="center" 
      android:text="@string/lblPais" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblJ" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblG" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblP" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPF" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPC" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblDif" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPuntos" /> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/lblPais1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="8" 
      android:gravity="center" 
      android:text="@string/lblPais" /> 

     <TextView 
      android:id="@+id/lblJ1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblG1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblP1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblPF1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblPC" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lbl0" /> 

     <TextView 
      android:id="@+id/lblDif1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPtsVacio" /> 

     <TextView 
      android:id="@+id/lblPuntos1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/lblPtsVacio" /> 

    </LinearLayout> 
</LinearLayout> 
+0

感謝您的解決方案!把所有TextView的widht設置爲0dip(就像Eclipse說的那樣,最好使用它而不是0dp)解決我的問題 –

+0

很高興幫助你... –

1

...

android:layout_height="wrap_content" 

android:layout_height="0dp" 

在所有TextView

+0

有與以進行0dp是'安卓layout_width'不是'安卓layout_height' –

+0

燁,如果你設置'機器人:方向=」水平「'然後設置'android:layout_width =」0dp「'否則'android:layout_height =」0dp「' –

1

你還沒有在第一個線性佈局android:weightSum="15"

0

您必須在第一個父級佈局上添加andoid:weightSum="15",並且所有內容都應該落實到位。

0

讓你的父母線性佈局的高度, android:layout_height="match_parent"

+0

這篇文章被自動標記爲低質量,因爲它太短了。你介意加入一些文字來解釋它是如何解決問題的嗎? – gung

相關問題