gpt4 book ai didi

android - ScrollView 下的静态按钮,其中包含动态表格布局

转载 作者:行者123 更新时间:2023-11-29 18:39:31 26 4
gpt4 key购买 nike

我有一个动态表,它可以只有 2 行,但也可以有多达 50 行。我试图将该表包装在 ScrollView 中。我还需要 2 个应显示在页面底部的按钮。

这是我正在努力实现但卡住了(附上图片以显示我正在尝试的东西):

  1. 如果行数很少,则不应有 ScrollView ,并且表格应显示在表格正下方的按钮上。
  2. 如果行数超过 x,将该表包装在具有最大高度的 ScrollView 中,然后在底部的静态位置显示按钮。表格滚动,但按钮在 ScrollView 下方消失。

有人可以建议我做错了什么吗?我在 StackOverflow 上浏览了很多问题,但找不到解决这种特殊情况的内容。

下面是我的布局文件,我在 Activity 中填充表格:

<?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:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/mainTable">
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="425dp"
android:layout_gravity="center_horizontal"
android:text="Submit" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>

Table and buttons appear fine when there are few rows

Max Height for the table with buttons

Buttons Disappear

RelativeLayout

enter image description here

最佳答案

我会考虑在 ScrollView 中使用 RecyclerView 而不是 TableLayout。如果您可以为按钮 View 布局设置静态高度,则可以操纵填充和边距以获得所需的效果。请参阅下面根据此答案修改的内容:https://stackoverflow.com/a/45161728/6723646

<?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:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="45dp">
<TableLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/mainTable">
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_marginTop="-45dp"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Submit" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>

关于android - ScrollView 下的静态按钮,其中包含动态表格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53345826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com