【问题标题】:How do I keep a button on screen which is below a listview when there are too many items in the list?当列表中有太多项目时,如何在列表视图下方的屏幕上保留一个按钮?
【发布时间】:2021-08-29 10:31:32
【问题描述】:

快速背景:我的部分活动处于相对布局中。我是故意这样做的,因为我想要一个直接位于列表视图下方的按钮。我希望按钮在列表视图展开时向下移动,这就是我以这种方式设置它的原因。我已经设置了列表视图的高度来包裹内容,这样在相对布局中,按钮会随着列表的展开而向下移动。

问题:一旦列表变得足够大以至于内容填满屏幕,按钮仍保留在列表下方(这很好)但我无法向下滚动列表以显示按钮。列表下方的按钮“消失”。我怎样才能做到这一点,以便我可以在列表/屏幕上滚动以显示我的按钮?

编辑:我确实希望按钮离开屏幕,我只是希望能够向下滚动以再次看到它。

下面的示例代码 + 图片: 3张图片,一张显示初始布局,接下来你可以看到按钮随着我的列表展开而移动,第三张,最终按钮到达底部,我无法滚动更多点击它。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/workoutList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/logExerciseButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/workoutList"
        android:text="@string/log_set" />

</RelativeLayout>

【问题讨论】:

  • 您可以使用 LinearLayout 或 ConstraintLayout 来执行此操作。RelativeLayout 存在性能问题
  • 哦,在这种情况下,相对布局是线性布局的子布局

标签: android android-layout listview android-listview


【解决方案1】:

将按钮作为页脚添加到列表视图会产生所需的行为:https://www.tutorialspoint.com/how-to-add-footer-in-android-listview

【讨论】:

    【解决方案2】:

    用 ScrollView 包装你的 ListView:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
            <ListView
            android:id="@+id/workoutList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
            
        </ScrollView>
        
        <Button
            android:id="@+id/logExerciseButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/scrollView"
            android:text="@string/log_set" />
    
    </RelativeLayout>
    

    【讨论】:

    • 哦,好吧,我试试这个,我以为列表视图默认是可滚动的。此外,随着列表的增加,这是否会使按钮能够向下移动?我想澄清一下,我确实希望按钮离开屏幕,但我希望能够向下滚动以再次查看它。
    • 嗯,是的,这不起作用,它现在只显示一个项目 - 我认为滚动视图卡在顶部,我只能滚动那一小部分
    【解决方案3】:
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ListView
            android:id="@+id/workoutList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/logExerciseButton" />
    
        <Button
            android:id="@+id/logExerciseButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/log_set" />
    
    </RelativeLayout>
    

    【讨论】:

    • 我仍然想要列表视图下方的按钮,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多