2011-12-23 39 views
1

不尊重我有这样的代码:重量比例在嵌套的LinearLayout

... 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="10"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="8"> 

    <TextView android:id="@+id/text2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    </LinearLayout> 
... 

此代码嵌套在其他的LinearLayout的具有相同weightSum和重量。 我使用的SDK是1.6 Android。我不知道为什么分配给权重等于1的元素的空间量为何不同,如果我删除中间的TextView。看起来,即使TextView的文字大小(字符数)更多,重量为1的LinearLayout也变小了。这怎么可能?

+0

你的weightSum是10,但加起来你的体重我得到12.如果我没有弄错,这会造成问题。 – 2011-12-23 17:37:52

+0

感谢您的回答..我很抱歉我的计划中的数字是正确的。 1 8 1.但仍然存在错误。 – 2011-12-27 09:23:42

+0

即使这个LinearLayout没有嵌套,并且即使将包含的TextView设置为固定宽度(以dp表示),问题仍然存在! LinearLayout真的可以不包含任何内容吗?我只需要在那里放一个宽度的百分比,并假装这个宽度保持不变。 – 2011-12-27 09:46:01

回答

3

我找到了解决方案。实际上,我需要将weight属性的每个容器的Layout_width设置为0px。

0

从我所知道的看来,你的weightSum应该是12而不是10. 第一个LinearLayout的权重= 2,第二个权重= 8,第三个权重= 2。

它可能会解决您的问题!

0

AFAIK,线性布局的重量并不是线性的。具有最大权重的组件赢得所有额外空间。为了玩弄布局,我强烈推荐最新的IntelliJ Idea - 即使免费的comminity版本也有android插件,并且非常有用的布局预览。

+0

好的..谢谢,但我的布局几乎完成。下次会考虑这个。 – 2011-12-27 10:12:07