1

我试图使用线性布局的layout_weight作为scrollview内部的高度。不过,我发现layout_weight只能用于fillViewPort = true,并且如果有足够的剩余空间可用于展开linear_weight。当ScrollView中的布局大于ScrollView时,layout_weight不起作用

我已经在下面列出了三个例子,我想让textView在#2和#3中与xml中的#1一样大小。 正如你可以在链接中看到的那样,在#2,#3中,textView被自动更改。

这里是我的例子。 TextViews和Buttons被包含在一个LinearLayout内部,然后在一个scrollView中。

1. Layout_weight相对于父项完美工作。 (工作)

http://s22.postimg.org/v0gl2tmyp/Screen_Shot_2013_06_26_at_4_30_51_PM.png

2. Layout_weight工作相对于剩余空间并且不是父。(不工作)

http://s22.postimg.org/sx65v5n5t/Screen_Shot_2013_06_26_at_4_30_44_PM.png忽略(不工作)

3 Layout_weight

http://s22.postimg.org/ibmaj5gu9/Screen_Shot_2013_06_26_at_4_40_32_PM.png

这是我的#3的xml代码。 #2和#1基本上是相同的代码,只有少量按钮或没有按钮。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/scroller" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weighSum="1" 
    android:fillViewport="true" > 

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

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 

     <Button 
      android:layout_width="30dip" 
      android:layout_height="30dip" 
      android:text="Accept" /> 


     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="6" 
      android:background="#AAABBB" 
      android:text="hello" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3" 
      android:background="#BBBAAA" 

      android:text="hello" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="5" 
      android:background="#AAABBB" 

      android:text="hello" /> 
    </LinearLayout> 

</ScrollView> 
+0

(工作案例)http://s22.postimg.org/v0gl2tmyp/Scr​​een_Shot_2013_06_26_at_4_30_51_PM.png –

+0

您可以为这三种方案添加xml吗?这将有助于更清楚你正在努力完成什么。 –

+0

什么不适合你?或者你想让其他人完成整个布局? –

回答

2

重量按照设计的方式工作。也许不是你希望他们“工作”的方式,但说他们“不工作”是不准确的。想一想你的第三种情况,你会期望TextView是什么样的大小?权重是可用空间的百分比(不是父级的总大小)。 scrollview和linearlayout至少是屏幕的大小,否则是所有子视图的总和。所以,如果你的孩子的看法超出屏幕的大小(包括那些没有权重),多少空间你希望将要分配给剩余的视图(与0dp和权重的)?问题的关键是空间量是任意的,Android将不会尝试假设你想要多大的空间,因为它知道没有可能的方式。

TL; DR - 您textviews设置了minHeight(以dp)来处理,你有太多的按钮的情况。

+1

是的,你是对的。我查看了线性布局的源代码,它仅适用于**剩余空间**,并且任何超出空间的布局都将设置为wrap_content。感谢您的答复。我希望有更好的方式来操作ScrollView中的子布局。 –

1

尝试在layout_weight的元素上设置layout_height =“0dp”。封闭的布局对象将使用任何可用空间来尽可能多地增长它们。

另外您对weightSum的使用有点疑问。通常weightSum应该> =所包含视图的总重量。您有一个weightSum,其中没有任何直接封闭的视图有重量,而另一个视图的总重量超过weightSum。第二种情况会导致奇怪的行为。

+0

嗨感谢您的回复。我和意见权重较大的比weighsum是因为要进行测试,看它是否能够使在滚动型较大的虚拟维度。但是,事实证明layout_weight只能使用**剩余空间**。所以超越界限只会被裁剪。 –

2

这些图像正是我期望为您的xml看到的。

这可能会帮助您:

  1. 当系统布局把View S使用的权重方法在屏幕上,它根据重量,而不是总分配只有备用空间空间。因此,为了让它根据权重分配总空间(这是大多数人直观预期的),您必须将您希望它的维度设置为0dip。因此,对于那些Button S和TextView是你需要设置

    android:layout_height = "0dip" 
    
  2. 您可能还需要强制ScrollView以填充视口停止一切蜷缩起来。见here

    android:fillViewport="true" 
    
  3. 个人观察。有时权重看起来反过来(即更小的权重给予更多的空间)。因人而异。

+0

嗨感谢您的回应。我把按钮放在那里,使布局更大,没有其他原因。我只对使父级布局的textViews比例感兴趣。另外,我编辑了代码,我不小心粘贴了错误的代码,但无论如何,情况#3不起作用。 –

+0

我有点困惑。您为布局3显示的图片正是我期望xml提供的内容。你在期待什么? –

+0

我希望textViews相对于重量和其父增长。因此,textView#1将增长60%,textView#2将增长30%,textView#3将增加50%的高度。 –