【问题标题】:Android UI: Multiple Horizontally Paged TableAndroid UI:多个水平分页表
【发布时间】:2011-06-22 22:16:33
【问题描述】:

我正在尝试实现一个 UI,其中包含使用 TableLayout 显示的数据表(动态填充)。我希望默认显示 4 列表格,并能够水平滚动表格以显示 4 个(或更多)附加列。这是否可以使用TableLayout 结合水平/垂直ScrollView?我将不胜感激任何展示这种风格的 XML 示例。

这是一个澄清的例子:

默认表格视图

向右滚动后查看

【问题讨论】:

    标签: android user-interface landscape tablelayout


    【解决方案1】:

    您应该能够通过将 TableLayout 放置在 Horizo​​ntalScrollView 中来实现这一点。在 TableLayout 中定义元素的宽度

    类似这样的东西(我没有测试过):

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/hsv1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
    
        <TableLayout android:layout_height="wrap_content" 
               android:gravity="center" 
               android:layout_width="wrap_content"  
               android:id="@+id/tl1" 
               android:scrollbars="horizontal">
    
            // Your other elements go here.
    
        </TableLayout>
    </HorizontalScrollView>
    

    【讨论】:

    • 这看起来不错,但我确实提到我正在动态添加元素(在我的活动类中)。如何通过以编程方式创建 TextView 并将其添加到 TableLayout 来指定要添加的元素的宽度?
    • @Ben Silver:只需设置表格的android:layout_width="wrap_content",并让表格行的动态插入使其拉伸。 HorizontalScrollView 应该处理剩下的事情。
    • 我还没有设法让它工作,我的数据表中的所有列都被压缩在一起,这是我的 XML 文件:pastebin.com/j7KuGtbM。如果您有时间查看它,请查看是否可以发现错误。抱歉,我对 XML 和 Android 很陌生。以下是我的 UI 在此布局下的外观:imgur.com/UcBkX
    • @Ben Silver - 一些事情:不确定是否需要 LinearLayout 包围 ScrollView。这可能会导致问题。此外,您还有一个包含内部 Horizo​​ntalScrollView 的外部 ScrollView。根据谷歌,您不应该将一个滚动视图放在另一个滚动视图中。找到另一种方法来做到这一点。因此,请尝试仅使用 Horizo​​ntalScrollView,然后使用 TableLayout,仅此而已。希望这会有所帮助。
    • @SBerg413 谢谢,非常感谢您的帮助。我合并了您的所有建议,以便将 TableLayout 包装在 Horizo​​ntalScrollView 中。 UI 看起来仍然与我链接的上一张图片相同。我想我将不得不找到一个替代解决方案。不过,感谢您抽出宝贵时间。
    【解决方案2】:

    我不确定这是否能直接解决您的问题,但请查看 github 上的 android-viewflow。 https://github.com/pakerfeldt/android-viewflow

    【讨论】:

      【解决方案3】:

      我解决这个问题的方法是使用ViewFlipper。我很惊讶以前没有人提到这一点。

      我将我的 XML 更改为如下所示:

      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                              android:id="@+id/hsv1"
                              android:layout_width="fill_parent"
                              android:layout_height="wrap_content">
              <ViewFlipper android:id="@+id/dataTableFlipper"
                       android:layout_width="fill_parent" android:layout_height="fill_parent">
      
                      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                  android:layout_width="fill_parent" 
                                  android:layout_height="wrap_content"
                                  android:id="@+id/dataTable"
                                  android:stretchColumns="*">
                      </TableLayout>
                      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                  android:layout_width="fill_parent" 
                                  android:layout_height="wrap_content"
                                  android:id="@+id/dataTable2"
                                  android:stretchColumns="*">
                      </TableLayout>
              </ViewFlipper>
      </ScrollView>
      

      这样我可以将表格的每一页作为单独的TableLayouts 进行管理。

      以编程方式声明 ViewFlipper 允许我使用 ViewFlipper.showNext() 在两个表之间交替。像魅力一样工作!

      【讨论】:

        猜你喜欢
        • 2018-06-27
        • 2019-10-27
        • 1970-01-01
        • 2017-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-15
        • 2017-11-29
        相关资源
        最近更新 更多