2010-03-10 123 views
52

所以我的布局基本上是这样的内部:添加视图布局底部的滚动型

<ScrollView> 
    <RelativeLayout> 
     <BunchOfViews/> 
     <ImageView android:layout_alignParentBottom="true"/> 
    </RelativeLayout> 
</ScrollView> 

我有ScrollView因此,所有的布局始终是可见无论屏幕的高度。问题是,在一个非常高的屏幕上,我仍然希望我的ImageView位于底部。然而,ScrollView的孩子似乎没有定义底部。 View放置在布局的顶部。我怎样才能以一种整洁的方式解决这个问题?

回答

115

我碰到了同样的问题。我从来没有找到一个令人满意的解决方案,但这是我如何做到的。也许别人有更好的方法,我讨厌添加不做任何事情的布局。

我的破解是在scrollview的底部添加一个虚拟linearlayout,它具有fill_parent来占用所有空间并强制滚动视图填满屏幕。然后将我想要的任何组件添加到该线性布局。

这里是我的布局,这是否之一:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:fillViewport="true" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="15px" > 

     <!-- bunch of components here --> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_below="@+id/spinner" 
      android:layout_marginTop="5px" 
      android:gravity="center_horizontal|bottom" 
      android:paddingTop="2px" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="20px" 
       android:paddingRight="20px" 
       android:text="Delete" /> 
     </LinearLayout> 
    </RelativeLayout> 
</ScrollView> 
+0

谢谢,我终于明白了。你已经让我的应用变得如此美丽! :) – pgsandstrom 2010-03-11 08:11:58

+0

@dweebo我不明白。它不适合我。你可以更具体的代码演示,并显示结束? – 2012-05-23 01:25:30

+35

android:fillViewport是技巧的重要组成部分,很容易错过。 – 2013-12-31 10:49:55

0

来源:http://code.google.com/p/k9mail/source/browse/k9mail/trunk/res/layout/account_setup_basics.xml?r=1314

这会帮助你:

<RelativeLayout 
     android:layout_marginTop="-45dip" 
     android:padding="0dip" 
     android:layout_alignParentBottom="true" 
     android:gravity="bottom|right" 
     android:background="@android:drawable/bottom_bar" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"> 
     <Button 
      android:id="@+id/manual_setup" 
      android:text="@string/account_setup_basics_manual_setup_action" 
      android:minWidth="@dimen/button_minWidth" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_marginBottom="-4dip" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="false" 
      /> 
     <Button 
      android:id="@+id/next" 
      android:text="@string/next_action" 
      android:minWidth="@dimen/button_minWidth" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:drawableRight="@drawable/button_indicator_next" 
      android:layout_marginBottom="-4dip" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="false" 
      /> 
    </RelativeLayout> 
+0

我很抱歉,但我不认为我是很清晰。我仍然希望imageview成为scrollview的一部分,就像我当前的布局一样。它应该在滚动视图的底部。如果scrollview比屏幕更短,那么imageview应该仍然在屏幕的底部。 – pgsandstrom 2010-03-10 14:22:05

+0

另外我不太明白负像素数字的影响。我从未使用过这些。所以也许我只是不明白你想说什么。如果是这样,你能否详细说明一下? :P谢谢。 – pgsandstrom 2010-03-10 14:24:04

+1

我不明白你为什么需要滚动视图,如果你的内容不填满屏幕。在视图上使用'android:gravity =“bottom | right”'应该放在底部。负值可能是为了切断按钮布局中的过多填充。 – Pentium10 2010-03-10 14:40:09

16

似乎LinearLayout中是没有必要的,所有这些都是重要的是fillViewPort。 你可以只使用

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottomParent="true"> 

现在你已经指定了RelativeLayout的是在屏幕的至少大小。

+6

这实际上是有效的。答案不够清楚 - 为'ScrollView'添加'fillViewPort =“true”'。 – 2012-12-03 08:00:42

57

我有同样的问题,找到了这个网页:

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

基本上,你设置了滚动的android:fillViewport为真,这将让孩子视图扩展到相同的高度滚动型本身,填补空间。然后您只需要将其中一个子控件'layout_height设置为fill_parentlayout_weight1,导致该控件“弹出”以填充空白空间。

请注意,如果ScrollView的内容已经足够高以填充ScrollView,则android:fillViewport不起作用,所以该设置仅在需要时启用。

我最后的XML看起来与此类似:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 
      <!-- this expands to fill the empty space if needed --> 
     </LinearLayout> 

     <!-- this sits at the bottom of the ScrollView, 
     getting pushed out of view if the ScrollView's 
     content is tall enough --> 
     <ImageView 
      android:layout_height="wrap_content" 
      android:src="@drawable/footer_image"> 
     </ImageView> 
    </LinearLayout> 
</ScrollView> 
+1

这很棒,但是填充屏幕的项目是一个EditText,并且在输入大量行的情况下,允许它在屏幕底部以下增长。有没有办法来防止这种情况发生,只有在软键盘显示时才能显示滚动视图? – finiteloop 2011-05-15 16:50:00

+1

这很棒。简洁整洁 – Maggie 2011-08-19 08:04:40

+0

谢谢! fillViewport = true很好地诀窍。 – 2014-07-02 17:02:00

0
ScrollView view = new ScrollView(this); 
ScrollView.LayoutParams lps = new FrameLayout.LayoutParams(FILL_PARENT, FILL_PARENT, Gravity.CENTER); 
LinearLayout layout = new LinearLayout(this); 
// what ever you want in the Layout 
view.addView(layout, lps); 
+0

你应该总是喜欢在XML中设置布局高度和宽度,避免在代码中进行。 – JaydeepW 2012-09-02 16:03:09

1

在你认为你想成为在底部采用Android:重力=“底”

4

乔尔·马龙的回答Adding view to bottom of layout inside a scrollview这是否有窍门?我的解决方案几乎相同,除了我使用Space小部件来完成在父布局内​​填充休息高度的工作。就像这样:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent 
    android:fillViewport="true" 
    > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_height="wrap_content" 
     > 
     <android.support.v4.widget.Space 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      /> 
    <ImageView 
     android:layout_height="wrap_content" 
     android:src="@drawable/footer_image" 
     /> 
    </LinearLayout> 
</ScrollView> 

注意:为废弃了很久

  • 在其他的答案fill_parent注意;
  • 比较为空LinearLayout填补父布局的其余部分,我想Space是更合适的(它的设计做到这一点的工作。)
  • 最后,不要忘了,在ScrollView开始重要的属性: android:fillViewport="true"。他们一起制造这个伎俩。
+0

它为我工作1 +。谢谢你节省我的时间 – Ninja 2017-07-17 13:03:19

+1

虽然最高评分的方法工作,这是我最喜欢的解决方案。使用空间是一个很好的技巧,它不需要额外的嵌套布局,但仍然适用于多个“底部连接”元素。 – 2017-10-18 13:10:38

+0

一句话:由于'layout_weight =“1”'被设置,Space的高度应该被设置为'“0dp”'。将高度设置为'“match_parent”'将显示相同的结果,但在Android Studio中显示警告。 – 2017-10-19 09:03:43

5

其对我来说工作完美机器人:fillViewport =“真”

相关问题