【问题标题】:Will a TableLayout supply a vertical scrollbar?TableLayout 会提供垂直滚动条吗?
【发布时间】:2012-03-13 03:54:43
【问题描述】:

或者我是否必须将 ScrollView 合并到 TableLayout 中?这甚至可能吗?我想为一个屏幕动态创建任意数量的行,但需要这些行是可滚动的。

我一直在徒劳地尝试使用 LinearLayout 让它工作太久了,我认为是时候转移到 TableLayout 或 RelativeLayout —— 任何 cmets 都比另一个更可取?

【问题讨论】:

    标签: android android-layout dynamic android-scrollview android-tablelayout


    【解决方案1】:

    您可以在滚动布局中使用表格布局,这是可能的。 例如,

    <ScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical" >
    
                <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >
    </LinearLayout>
    
    </ScrollView>
    

    您可以使用表格布局而不是线性布局..

    【讨论】:

    • 是的,我知道我可以使用 TableLayout;我的问题是,当屏幕上显示的行数过多时,TableLayout会添加垂直滚动条吗?
    • @Clay Shannon:是的,它会起作用的……试试吧……如果没有,请告诉我……
    【解决方案2】:

    “列表”样式的视图都不会自动滚动。 LinearLayout、TableLayout、RelativeLayout,它们都没有这样做,因为(就像其他人指出的那样)将它们包装在 ScrollView 中非常容易,这只是它们不需要的功能。

    【讨论】:

    • 好的,谢谢;我将发布我的理论布局,看看是否有人向它扔泥巴。
    【解决方案3】:

    您必须为您的TableLayout 指定一个ScrollView,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:background="#ffffff"
        android:layout_height="fill_parent">
    
        <RelativeLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff">
    
            <TableLayout 
                android:id="@+id/myTableLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:shrinkColumns="1"/>     
    
        </RelativeLayout>
    
    </ScrollView>
    

    【讨论】: