始终在屏幕底部放置一个固定高度的按钮

时间:2012-03-07 14:07:35

标签: android android-layout scrollview

我希望在“活动”屏幕的底部保留一个按钮。无论其上方的滚动视图的大小如何,都必须进行修复。问题是,一旦滚动视图的文本视图占据某个位置,我的按钮的高度会不断下降,最终会被推出活动屏幕。这是我正在使用的布局。

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

    <TextView android:id="@+id/tvBanner" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" android:gravity="center"
        android:text="Practice Exam" />


    <ScrollView android:id="@+id/Scroll" android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout android:id="@+id/LinearLayout1"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView android:id="@+id/tvDesc" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_margin="10dp"
                android:gravity="center" android:paddingTop="40dp" android:text="@string/welcom"
                android:textSize="20sp" />

            <TextView android:id="@+id/tvURL" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_marginBottom="30dp"
                android:paddingTop="10dp" android:layout_gravity="center"
                android:text="hello" android:textColor="@android:color/white"
                android:typeface="sans" />
        </LinearLayout>
    </ScrollView>

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:gravity="bottom">

        <Button android:id="@+id/btBottom" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_weight="1"
            android:text="Enter" />

    </RelativeLayout>



</LinearLayout>

我也尝试在Scrollview中使用android:weight = 1和android:layout_height = 0dp。但这会从我的活动中删除整个Scrollview部分,我看不到任何内容。

我知道有很多类似的问题要问这个并相信我,我已经尝试过很多这样的问题。但是,没有一个技巧对我有用。我花了差不多半天来解决这个问题。请帮助。

6 个答案:

答案 0 :(得分:2)

对于这样的情况,请始终使用RelativeLayouts。 LinearLayout不适用于此类用法。

试试这个:

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"         
    android:layout_centerHorizontal="true">

    <Button android:id="@+id/btBottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Enter" 
        android:layout_alignParentBottom="true"/>

   <ScrollView
      ...
      android:layout_above="@id/btnGetMoreResults"/>
</RelativeLayour>

答案 1 :(得分:1)

你应该尝试使用RealativeLayout而不是Linear,然后你可以使用

android:layout_above="@+id/btBottom"
 android:layout_alignParentBottom="true"

那应该可以解决你的问题。

答案 2 :(得分:0)

看起来你的按钮中需要'alignParentBottom',就像这样

    <Button android:id="@+id/btBottom" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" 
        android:text="Enter" />

答案 3 :(得分:0)

  1. 不要将Button包裹到RelativeLayout
  2. ScrollView的layout_height设置为0dp
  3. ScrollView的layout_weight添加到1

答案 4 :(得分:0)

为此,如果您将RelativeLayout用于外部包装器布局,则可能是个好主意。然后,您可以使用内部按钮将布局设置为LinearLayout,并在将layout_alignParentButtom="true"设置为@+id/Scroll

时设置layout_alignParentTop="true"

此外,您应该将布局内部的高度设置为wrap_content而不是fill_parent

答案 5 :(得分:0)

为什么你在顶级使用LinearLayout? 选择带有填充/填充的RelativeLayout和以下三个孩子(不再嵌套!):

1)包含android:layout_alignParentTop="true"android:layout_centerHorizontal="true"

的TextView

2)带有android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"

的按钮

3)使用android:layout_below=@id/textview_idandroid:layout_above="@id/button_id"

的ScrollView

虽然我没有测试过,但试一试。