2014-07-16 32 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

相关问题