【问题标题】:Impossible to add a footer to a ScrollView - android无法向 ScrollView 添加页脚 - android
【发布时间】:2015-11-24 23:59:15
【问题描述】:

我目前正在开发一个移动应用程序,当我尝试在滚动视图下放置页脚时遇到问题。

这是我的代码:

<RelativeLayout
    android:id="@+id/RelativeLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">



    <RelativeLayout

        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottomcontent"
        android:orientation="vertical"
        android:background="@drawable/border">
            //the footer is added dynamically
    </RelativeLayout>

<ScrollView
    android:layout_weight="1"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/contentcontainer">
        <LinearLayout
            android:id="@+id/scrollcontentcontainer"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            //the content is added dynamically from a layout template
        </LinearLayout>
    </LinearLayout>
</ScrollView>
</RelativeLayout>

滚动视图的内容是一组相对布局,里面有一些按钮和文本视图。它基于我多次膨胀的布局。

页脚只是一个带有一些按钮的线性布局。

问题是我尝试了在 Internet 上找到的所有不同解决方案,但都没有奏效。在我的情况下,页脚卡在滚动视图的内容下方,而不是滚动视图本身下方,所以我必须向下滚动,直到内容结束才能到达我的页脚。但是页脚应该保留在屏幕底部...

我也尝试了这些解决方案,但没有任何效果: - http://www.javacodegeeks.com/2013/10/android-fixed-header-and-footer-with-scrollable-content-layout-example.html (当我尝试这个时,我在屏幕顶部有一个页脚,没有别的......) - http://www.byteslounge.com/tutorials/android-fixed-header-and-footer-with-scrollable-content-layout-example 和其他一些(效果不佳!)

我还尝试了所有可能的方法,例如使用重力、重量、fillViewPort、对齐底部……但不可能得到预期的结果。 最低API设置为14,我用的是android studio。

感谢大家的帮助!

编辑1: 可绘制边框 `在此输入代码

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="-2dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#000000" />

            <solid android:color="#3b010101" />

            <padding android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

</layer-list>

【问题讨论】:

  • 请检查我的回答,如果能解决问题,请告诉我?
  • 将android:layout_above="@id/bottomcontent" 添加到您的ScrollView
  • @JaredRummler 完成,仍然无法正常工作(页脚卡在顶部)...

标签: android android-layout layout android-studio android-scrollview


【解决方案1】:

您可以尝试以下方法,我在 RelaveLayout 中添加 ScrollView 时也遇到了麻烦。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:id="@+id/bottomcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/border">
        <!---add something there eg:-->

      <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Test"/>

     </RelativeLayout>


    <ScrollView
        android:id="@+id/scroller"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/bottomcontent"
        android:layout_alignParentTop="true">

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

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

            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

.

我在this documentation 中找到了以下内容。这肯定会导致问题

注意:在平台版本 17 及更低版本中,RelativeLayout 受 可能导致子视图被测量的测量错误 MeasureSpec 值不正确。 (参见 MeasureSpec.makeMeasureSpec 更多详细信息。)这是在使用 RelativeLayout 容器时触发的 放置在滚动容器中,例如 ScrollView 或 水平滚动视图。如果自定义视图没有正确配备 使用 MeasureSpec 模式测量 UNSPECIFIED 被放置在 RelativeLayout,这无论如何都会像RelativeLayout一样默默地工作 而是通过一个非常大的 AT_MOST MeasureSpec。

对于设置 android:targetSdkVersion="17" 或在其清单的 uses-sdk 中更早 兼容性标签。以 SDK 版本 18 或更高版本为目标的应用程序将 接收正确的行为

【讨论】:

  • 遗憾的是,我的页脚仍然在屏幕顶部,没有任何其他内容...
  • @Virthuss 这在我的 IDE 上运行良好,你有任何错误吗?你能添加边界drawable吗?
  • 完全没有错误...请原谅我糟糕的英语,但边界可绘制是什么意思?
  • 这部分可以加吗,android:background="@drawable/border"
  • 你能直接将我的答案 xml(整个东西)复制到你的 xml 中吗?只是为了检查?因为,我无法重现这个问题
猜你喜欢
  • 2014-10-25
  • 2014-12-14
  • 2014-08-17
  • 2021-07-06
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
  • 2011-03-25
  • 2015-04-05
相关资源
最近更新 更多