2011-12-30 114 views
4

我想让ScrollView在屏幕底部留出太多空间,因为它会让我的两个按钮不能显示。我也不想为ScrollView手动设置高度。保持ScrollView占用太高的高度

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     ... 
     android:orientation="horizontal"> 
     <Button .../> 
     <TextView .../> 
     <Button .../> 
    </LinearLayout> 
    <ScrollView 
     android:layout_width="math_parent" 
     android:layout_height="wrap_content"> 
     <ImageView .../> 
     <ImageView .../> 
     <ImageView .../> 
    </ScrollView> 
    <LinearLayout 
     ... 
     android:orientation="horizontal"> 
     <Button .../> 
     <Button .../> 
    </LinearLayout> 
</LinearLayout> 

这是我的布局的通用版本。正在发生的事情是ScrollView一直延伸到屏幕的底部,无论它有多少物品。这会导致屏幕底部的两个按钮不可见。

如何在不手动设置ScrollView高度的情况下阻止发生这种情况?

我用我所有的观点使用android:layout_height="wrap_content"

是不是应该自动分配视图的高度来适应屏幕的高度?

(希望包括图像,但我的代表处是不够高,但( - _ - ))

+1

这可能是切换到的RelativeLayout作为根布局的好主意。它应该为您提供工具,让ScrollView保持在视图的中心区域内,而无需自己明确计算其高度。 – harism 2011-12-30 15:06:37

+0

感谢您的评论。我已经实施了一个不同的解决方案,但是我一路上学到了更多的东西,我相信你的实现将最适合我想做的事情。一个RelativeLayout将允许我将Buttons锚定到屏幕底部,然后将所有其他UI对象放置在屏幕顶部和按钮之间。 – edc598 2012-02-01 03:51:28

+0

你的ScrollView应该只有一个孩子。不知道这是不是你的问题的原因。 – Wytze 2014-04-14 14:20:37

回答

2

的方式我今天解决这个问题的方法是使用RelativeLayout作为基地。将两个LinearLayout分别固定到RelLayout的顶部和底部。然后我会插入ScrollView,但我会确保设置它的布局属性如下:

android:layout_below="topLinearLayout" android:layout_above="bottomLinearLayout"

+0

不应该是“alignParentTop = true”(底部相同) – ataulm 2013-09-16 20:33:39

+0

是的顶部LinearLayout你会把'alignParentTop = true'和底部的LinearLayout你会'alignParentBottom = true',但对于ScrollView我的方式它是否如上所述,换句话说,使ScrollView锚点的顶部边缘到顶部LinearLayout的底部边缘,底部边缘到底部LinearLayout的顶部边缘。 – edc598 2013-09-16 21:02:21

1

您是否尝试过使用layout_weightScrollView。 IIRC中,如果为一个元素设置了非零权重(并且对应的维度设置为0dp),那么在为同级视图设置测量值(在此情况下为LinearLayout)之后,它将填充该维度中的剩余空间在ScrollView的顶部和底部。

道歉,没有得到一个Android环境检查,它已经有一段时间。

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

    <LinearLayout> 
     ... 
     Something at the top 
     ... 
    </LinearLayout> 

    <ScrollView 
     android:layout_width="match_parent" 
     **android:layout_height="0dp" 
     android:layout_weight="1"**> 
     <ImageView .../> 
     <ImageView .../> 
     <ImageView .../> 
    </ScrollView> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 
     ... 
     buttons 
     ... 
    </LinearLayout> 
</LinearLayout> 

Here's an answer与有关layout_weight属性的详细信息。