2010-06-27 65 views
29

我遇到的问题似乎是,如果我有一个视图(例如myButton)内的RelativeLayout设置为alignParentBottom - (根据下面的代码) - 然后用scrollview包装RelativeLayout(必要的内容将是在较小的屏幕上或在方向改变为横向时可见) - 然后,当父级底部不可见时(例如,当更改为横向时)myButton显示在页面的顶部 - 而不是底部。Android的RelativeLayout:如何alignParentBottom包装在一个ScrollView?

如何将视图与屏幕底部对齐,并使底部保持在底部,即使底部位于滚动条下方?

<ScrollView 
android:id="@+id/mainScrollView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:fillViewport="true"> 



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/topLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 



... lots of views and stuff 


<Button android:layout_alignParentBottom="true" 
android:id="@+id/myButton" 
android:text="Click Me" /> 

</RelativeLayout> 
</ScrollView> 
+0

你知道怎么今天这个做什么? – 2016-09-04 19:08:45

回答

6

使用fill_parent作为ScrollView的子项的高度没有意义。您告诉RelativeLayout始终与其父级一样高。在这种情况下,ScrollView变得毫无用处! RelativeLayout的高度应该设置为wrap_content,在这种情况下,根据RelativeLayout的含义,alignParentBottom可能无法按预期工作。你应该简单地使用一个LinearLayout,它会简单得多。

+0

嗯,我不明白你为什么说scrollview变得没用,它的功能是允许滚动,并且它这么做......所以我必须使用它。我必须使用RelativeLayout,以便我可以使用android_alignParentBottom。我看不出如何使用LinearLayout解决此问题,因为我无法将myButton对齐到屏幕底部。是否无法将视图对齐到屏幕底部并同时支持滚动? – JohnRock 2010-06-27 15:33:13

+0

如果孩子的身高= fill_parent,则无用。 fill_parent的意思是“我想和我的父母一样高。”因此,如果ScrollView的内容与ScrollView本身一样大,它将不会滚动。 – 2010-06-28 00:55:13

+0

好的。我感谢你的指导。但是,布局确实滚动,这不是我想解决的问题。 – JohnRock 2010-06-28 02:25:18

3

很好,你可以试试这个

<LinearLayout android:id="@+id/parentLayout" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <ScrollView android:id="@+id/mainScrollView" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1" xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fillViewport="true"> 



     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/topLayout" android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 



      ... lots of views and stuff 


      <Button android:layout_alignParentBottom="true" android:id="@+id/myButton" 
       android:text="Click Me" /> 

     </RelativeLayout> 
    </ScrollView> 
</LinearLayout> 
+0

谢谢你的工作 – 2015-05-27 12:09:12

+0

谢谢哥们!我面临同样的问题,并开始认为这是不可能的。你的解决方案可以帮助我很多 – Androider 2016-01-19 15:39:42

24

就拿按钮出了滚动型的,裹无论是滚动型和按钮的线性布局,设置了滚动的高度0dip并设置它的权重为1,不要为该按钮设置任何重量属性,因此滚动视图可以占用所有剩余空间,只保存按钮在底部的空间方式。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

     ....lots of views... 


     </LinearLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:text="Button" /> 

</LinearLayout> 
+7

但是,即使“ScrollView”是可滚动的,这会使'Button'始终保持在屏幕的底部,对吗? – 2014-07-30 09:24:11

+0

是的它会:D – kunmi 2015-07-26 04:10:52

+0

这是我喜欢的解决方案,但要小心“体重”对性能不利 – superuser 2016-05-17 05:24:05

-1

这是一个有点晚,但simpliest解决方案是增加

android:below="@id/lots_of_stuffs" 

像这样:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainScrollView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true"> 

    <RelativeLayout 
    android:id="@+id/topLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 


    <LinearLayout 
     android:id="@+id/lots_of_stuffs" > 

     ... lots of views and stuff 

    </LinearLayout> 

    <Button android:layout_alignParentBottom="true" 
     android:id="@+id/myButton" 
     android:text="Click Me" 
     android:below="@id/lots_of_stuffs" /> <!-- Here is the "hack" --> 

    </RelativeLayout> 
</ScrollView> 
+0

有点晚了,仍然不是一个合适的解决方案,这将拉伸按钮到其填充高度。 – Shivansh 2016-09-12 17:01:47

相关问题