Android RelativeLayout : how to alignParentBottom when wrapped in a ScrollView?
The problem I am having seems to be that if I have a view (eg 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.
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>
将按钮从SCrollview中移出,将滚动视图和按钮都包含在线性布局中,将滚动视图的高度设置为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>
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.
那么你可以试试这个
<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>
链接地址: http://www.djcxy.com/p/16642.html
上一篇: 容器和视图之间的宽度/高度
下一篇: Android的RelativeLayout:如何alignParentBottom包装在一个ScrollView?