ScrollView会在其下方保留重叠项目

时间:2014-12-26 18:05:28

标签: android xml button scrollview

我有一个ScrollView,下面是两个水平对齐的按钮。当滚动视图不填满整个窗口时,我希望按钮为A)位于窗口底部; B)当滚动视图大于窗口时,保持位于滚动视图下方。

目前,当滚动视图没有填满整个窗口时,按钮成功地放置在屏幕的底部,但是当滚动视图有太多数据无法放入窗口时(例如,当手机转到横向视图)滚动视图数据重叠在按钮上。

我很难通过使用XML来确定如何同时满足这两个条件。也许我别无选择,只能介绍一些程序化检查?无论如何,这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <TextView android:id="@+id/groupHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:textColor="#000000"
        android:textSize="25sp"
        android:text="@string/groupHeader" />     


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/groupHeader" >

        <LinearLayout
            android:layout_gravity="right"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/groupCreate" >
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/groupcreate_button_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:text="@string/groupcreate_button_new" />

    <Button
        android:id="@+id/groupcreate_button_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="5dp"
        android:text="@string/groupcreate_button_next" />

</RelativeLayout>

感谢您提前提供的所有帮助。

编辑:

我应该提一下LinearLayout&#34; groupCreate&#34;在有问题的ScrollView中创建为空,但是立即以编程方式添加了视图,这导致它占用空间并且有时会溢出。

3 个答案:

答案 0 :(得分:1)

查看ScrollView的fillViewPort属性。 Romain Guy很好地证明了它的用法on his blog

答案 1 :(得分:0)

试试这个xml

我在您的xml中进行了一些更改,包括id和其他内容,只是为了在我的设备上进行测试。 之后你可以根据你的情况改变。

<LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#FFFFFF"
       android:orientation="vertical">

    <TextView
        android:id="@+id/groupHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:gravity="center"
        android:text="Group Header"
        android:textColor="#000000"
        android:textSize="25sp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        // You can put other View inside this scrollview

    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/groupcreate_button_new"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New" />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/groupcreate_button_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next" />

    </LinearLayout>

</LinearLayout>

希望这可以解决您的问题。

答案 2 :(得分:0)

我自己遇到了这个问题。 @ A.R的解决方案正在工作,但我感觉缺少一些解释。您必须使用 LinearLayout 才能将 android:layout_weight 添加到子视图中,这具有所有的“魔力”。向两个视图添加android:layout_weight = 1会强制android渲染两个视图,并排除重叠。 示例:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/groupcreate_button_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>