【问题标题】:TableLayout being truncated when placed inside ScrollView放置在 ScrollView 中时 TableLayout 被截断
【发布时间】:2016-08-19 16:17:34
【问题描述】:

我有一个 TableLayout 动态填充了多行数据。它包含的数据比屏幕所能容纳的多,所以我要求它是可滚动的。

我的问题是,当我将 TableLayout 放在 ScrollView 中时,它似乎从 TableLayout 的顶部切断了许多行。

作为参考,完整的代码在这里(问题是已经杂乱无章以适应一切)http://pastebin.com/w7Fi3Bzz

请参阅下面与我遇到的问题相关的代码

这是没有 ScrollView 的 XML:

        <TableLayout
            android:orientation="vertical"
            android:fillViewport="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:id="@+id/tbl_statistics"
            android:stretchColumns="*"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            >
        </TableLayout>

它是什么样子的:

这是带有封闭 ScrollView 的同一个 XML:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    >

        <TableLayout
            android:orientation="vertical"
            android:fillViewport="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:id="@+id/tbl_statistics"
            android:stretchColumns="*"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            >
        </TableLayout>

</ScrollView>

这是表格的样子,您可以看到表格顶部被截断(填充了完全相同的数据):

我在这里LinearLayout not expanding inside a ScrollView 发现了类似的问题,但答案对我没有帮助。

【问题讨论】:

  • 第二个布局中不需要LinearLayout
  • @MikeM。无论我是否包含它,这个问题仍然存在
  • 我第二个@MikeM。 LinearLayout 是多余的。当您删除 LinearLayout 时,您应该将 layout_heightTableLayout 设置为 wrap_content
  • 尝试删除TableLayout 中的layout_gravity 属性,不带LinearLayout。此外,fillViewportScrollView 以外的任何内容都无用。
  • 好吧,伙计们,我将删除它以简化我的问题,但我已经尝试了两种方式,但它仍然被截断

标签: android xml scrollview tablelayout


【解决方案1】:

在你的current layout:

  1. TableLayout 元素中删除android:layout_gravity="center"

  2. android:layout_weight="1" 添加到ScrollView 元素。

  3. 清理并重建您的项目。

此外,fillViewport 属性仅适用于 ScrollViews。

【讨论】:

  • 做到了。谢谢。
  • 我将奖励我的赏金我首先要部署我的应用程序并验证它在生产中是否有效。感谢您在过去 2 天内提供的所有帮助。
  • 没问题!对不起,你不得不花费 50 代表。我会回来帮忙的。 :-) 刚好在赏金中看到你的问题。希望它最终为您解决所有问题。干杯!
【解决方案2】:

我仅通过将 TableLayout 插入到 LinearLayout 中解决了相同的问题,LinearLayout 是 ScrollView 的唯一子级。我尝试了所有其他变体,但均未成功(ScrollViewfillViewportTableLayoutandroid:layout_weight="1")。

我使用TableLayout 动态插入Views 到它

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TableLayout
            android:id="@+id/activity_displayFields_tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp" />
    </LinearLayout>
</ScrollView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多