Android RelativeLayout:包装在 ScrollView 中时如何对齐ParentBottom?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3126347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android RelativeLayout : how to alignParentBottom when wrapped in a ScrollView?
提问by JohnRock
The problem I am having seems to be that if I have a view (e.g. myButton) inside a RelativeLayout set to alignParentBottom - (as per the code below) - and then wrap the RelativeLayout with a scrollview (necessary so the content will be viewable on smaller screens or when orientation changes to landscape) - then, when the parent bottom is NOT visible (for example when changed to landscape orientation) myButton is displayed at the TOP of the page - NOT the bottom.
我遇到的问题似乎是,如果我有一个相对布局中的视图(例如 myButton)设置为 alignParentBottom -(按照下面的代码) - 然后用滚动视图包装相对布局(这是必要的,这样内容就可以在较小的屏幕或当方向更改为横向时) - 然后,当父底部不可见(例如,当更改为横向时) myButton 显示在页面顶部 - 不是底部。
How can you align a view to the bottom of the screen and have it remain at the bottom even if the bottom is below the scroll?
如何将视图与屏幕底部对齐并使其保持在底部,即使底部在滚动下方?
<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>
回答by kunmi
Take the button out of the SCrollview, Wrap both the Scrollview and the Button in a Linear Layout, set the scrollview's height to 0dip and set it's weight to 1, do not set any weight property for the button, so the scrollview can occupy all the remaining space saving only the button's space way at the bottom.
将按钮从 SCrollview 中取出,将 Scrollview 和 Button 都包裹在一个 Linear Layout 中,设置 scrollview 的高度为 0dip 并设置它的权重为 1,不要为按钮设置任何 weight 属性,这样滚动视图就可以占据所有剩余空间仅节省底部按钮的空间方式。
<?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>
回答by Romain Guy
Using fill_parent as the height of a child of a ScrollView is meaningless. You are telling the RelativeLayout to be always as tall as its parent. In this case, the ScrollView becomes useless! The height of the RelativeLayout should be set to wrap_content, in which case, depending on what the RelativeLayout contains, alignParentBottom may not work as you expect. You should simply use a LinearLayout, it will be much much simpler.
使用 fill_parent 作为 ScrollView 的孩子的高度是没有意义的。您告诉 RelativeLayout 始终与其父级一样高。在这种情况下,ScrollView 变得无用了!RelativeLayout 的高度应设置为 wrap_content,在这种情况下,根据 RelativeLayout 包含的内容,alignParentBottom 可能无法按预期工作。您应该简单地使用 LinearLayout,它会简单得多。
回答by pankajagarwal
well you could try this out
你可以试试这个
<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>
回答by KaMyLL
None of these solutions were satisfying for me, so I'll post my own solution.
这些解决方案都没有让我满意,所以我会发布我自己的解决方案。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- lots of stuff -->
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
Little explanation: when there is a lot of space (f.e. in portrait) Space view will fill that space and push Button down. When there is no space (f.e. in landscape) Space view will have 0 height and Button will be directly below content.
小解释:当有很多空间时(纵向中的fe)空间视图将填充该空间并按下按钮。当没有空间时(横向为 fe)空间视图的高度将为 0,按钮将直接位于内容下方。
回答by kristyna
It's a little bit late, but the simpliest solution is to add
有点晚了,但最简单的解决方案是添加
android:below="@id/lots_of_stuffs"
Like this:
像这样:
<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>