Android RelativeLayout : how to alignParentBottom when wrapped in a ScrollView?
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开发者_StackOverflow社区 page - NOT the bottom.
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>
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.
<?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>
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.
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>
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.
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>
精彩评论