【问题标题】:How to make a scrollable TableLayout?如何制作可滚动的 TableLayout?
【发布时间】:2011-06-28 22:15:04
【问题描述】:

请看这里的 XML 代码:

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- Some stuff goes here -->

    />
    </TableRow>

    <TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- Some stuff goes here -->

    />
    </TableRow>

    <TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- Some stuff goes here -->

    />
    </TableRow>


</TableLayout>

我的代码比这长得多,但我只是删除了不必要的部分。 问题是我想让这个TableLayout 可以滚动,这样我的所有东西都可以显示出来。

我试图将这一行放在TableLayout 中以使其可滚动:

android:isScrollContainer="true"

但它不能完成这项工作。 有什么办法吗?

【问题讨论】:

    标签: android scroll android-tablelayout


    【解决方案1】:

    把整个东西包起来:

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="none"
        android:layout_weight="1">
        <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
    
        ...
    
    </ScrollView>
    

    【讨论】:

    • @citizen 为什么是LinearLayout
    • @Sudhanshu 这篇文章是 3 年前的,但据我所知,它是将方向设置为垂直的最基本的包装器。
    • 如果您也需要水平滚动,请参阅stackoverflow.com/questions/16623337/…
    • 在我的情况下它没有显示最后几行(不要滚动到那里)
    【解决方案2】:

    从技术上讲,您不需要 ScrollView 中的 LinearLayout:

    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    android:layout_weight="1">
    
        <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:isScrollContainer="true">
    
        <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
            <--!Everything Else You Already Have-->
    
        </TableRow>
     </TableLayout>
    </ScrollView>
    

    一旦你在 ScrollView 中占据了足够的空间,滚动效果就会激活(有点像 HTML TextArea,一旦你有足够的文本行,滚动就会激活。)

    您也可以嵌套 ScrollView,但是在 ScrollView 中有足够的内容之前,您无法感受到滚动效果。

    【讨论】:

    【解决方案3】:

    感谢 mbauer,它解决了我的问题 我下单了

    1. 表格布局
    2. 带结尾的TableRow(用于列的标题)
    3. 滚动视图
    4. 线性布局
    5. x 表行结束
    6. 结束线性布局
    7. 结束滚动视图
    8. 结束表格布局

    【讨论】:

    • 不要工作! “2.Table Row”中的列宽不等于“5.x TableRow”中的列宽
    【解决方案4】:

    它也适用于约束布局。只需在 TabLayout 上添加以下属性。

    <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:isScrollContainer="true">
    
            . . .
    

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      相关资源
      最近更新 更多