【问题标题】:How to add scrollview in Table Layout?如何在表格布局中添加滚动视图?
【发布时间】:2017-08-14 01:01:14
【问题描述】:

这是我第一次使用 Scrollview,我想用 tablelayout 3x8 行制作应用程序。行的高度和宽度相同,表格可以垂直滚动。我怎么能在 xml 布局中做到这一点?感谢您的帮助

<ScrollView android:id="@+id/layout"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_marginTop="5dip"
    android:scrollbarStyle="outsideInset"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="*"
    >
    <TableRow
        android:layout_weight="1"
        android:gravity="center">
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
    </TableRow>
    <TableRow
        android:layout_weight="1"
        android:gravity="center">
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
    </TableRow>
    <TableRow
        android:layout_weight="1"
        android:gravity="center">
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
    </TableRow>
    <TableRow
        android:layout_weight="1"
        android:gravity="center">
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
    </TableRow>
</TableLayout>

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="*"
    >
    <TableRow
        android:layout_weight="1"
        android:gravity="center">
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
    </TableRow>
    <TableRow
        android:layout_weight="1"
        android:gravity="center">
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
    </TableRow>
    <TableRow
        android:layout_weight="1"
        android:gravity="center">
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
    </TableRow>
    <TableRow
        android:layout_weight="1"
        android:gravity="center">
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
        <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
    </TableRow>
</TableLayout>
    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 使用 TabLayout 属性看看 :app:tabMode="scrollable" 可能会有帮助。。
  • 谢谢先生,它解决了我的部分问题@User6829548
  • 欢迎@Denny..

标签: android scrollview tablelayout


【解决方案1】:

这是您必须手动指定 TableRow 高度的方法之一,以便您可以按如下方式实现此目的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ScrollView android:id="@+id/layout"
        android:layout_height="match_parent"
        android:scrollbars="horizontal|vertical"
        android:layout_width="match_parent"
        android:layout_marginTop="5dip"
        android:scrollbarStyle="outsideInset"
        android:fillViewport="true"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout
            android:layout_width="match_parent" android:orientation="vertical"
            android:layout_height="match_parent">

            <TableRow
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
            </TableRow>
            <TableRow
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
            </TableRow>
            <TableRow
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
            </TableRow>
            <TableRow
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
            </TableRow>
            <TableRow
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
            </TableRow>
            <TableRow
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
            </TableRow>
            <TableRow
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
            </TableRow>
            <TableRow
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <Button android:layout_weight="1" android:text="Button" android:layout_width="match_parent" android:layout_height="match_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
                <Button android:layout_weight="1" android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
            </TableRow>

        </LinearLayout>
    </ScrollView>

</LinearLayout>  

实现此目的的另一种方法是使用GridViewRecyclerViewRecyclerView 是实现这一目标的最简单方法。我希望它对你有用。谢谢你

【讨论】:

  • 谢谢您,先生,这对我有很大帮助。
【解决方案2】:

ScrollView 中删除LinearLayout 并尝试,如果不起作用,请使用此

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="200dp" >
            <EditText
                android:layout_weight="1"
                android:background="#F00"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#FF0"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#F0F"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="100dp">
            <EditText
                android:layout_weight="1"
                android:background="#F00"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#FF0"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#F0F"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="100dp">
            <EditText
                android:layout_weight="1"
                android:background="#F00"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#FF0"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#F0F"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="100dp">
            <EditText
                android:layout_weight="1"
                android:background="#F00"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#FF0"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#F0F"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="100dp">
            <EditText
                android:layout_weight="1"
                android:background="#F00"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#FF0"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#F0F"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="100dp">
            <EditText
                android:layout_weight="1"
                android:background="#F00"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#FF0"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <EditText
                android:layout_weight="1"
                android:background="#F0F"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableRow>
    </TableLayout>
</ScrollView>

【讨论】:

  • 谢谢您,先生,我尝试删除我的线性布局,但它不起作用。谢谢你的回答
【解决方案3】:

不要使用relative layout 以外的任何布局。我不知道它是如何工作的,但它确实有效。[至少对我来说]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    相关资源
    最近更新 更多