【问题标题】:How to create a table in Android with multiple columns?如何在 Android 中创建具有多列的表?
【发布时间】:2011-03-08 05:55:52
【问题描述】:

我想在 android 中创建一个包含多列的表。我看到的大多数示例都是 2 列。 (我是 Java 和 Android 的新手。)我需要 3-4 列,我应该能够在表中动态添加行。谁能给我一个示例代码。 (我在win 7中使用eclipse)

【问题讨论】:

    标签: java android xml android-tablelayout


    【解决方案1】:

    我假设您说的是 TableLayout 视图而不是数据库中的表??

    如果是这样,下面是一个三列三行表的 XML 示例。

    每个 元素在表格中创建一行,元素内的每个视图创建一个“列”。我用过 TextViews,但它们可以是 ImageViews、EditText 等。

    <?xml version="1.0" encoding="utf-8"?>
    
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id = "@+id/RHE"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="0"
             android:padding="5dp">
    
         <TableRow android:layout_height="wrap_content">
             <TextView
                 android:id="@+id/runLabel"
                 android:text="R"
                 android:layout_height="wrap_content"
                 />
             <TextView
                 android:id="@+id/hitLabel"
                 android:text="H"
                 android:layout_height="wrap_content"
                 />
             <TextView
                 android:id="@+id/errorLabel"
                 android:text="E"
                 android:layout_height="wrap_content"
                 />
         </TableRow>
    
         <TableRow android:layout_height="wrap_content">
             <TextView
                 android:id="@+id/visitorRuns"
                 android:text="0"
                 android:layout_height="wrap_content"
                 />
             <TextView
                 android:id="@+id/visitorHits"
                 android:text="0"
                 android:layout_height="wrap_content"
                 />
             <TextView
                 android:id="@+id/visitorErrors"
                 android:text="0"
                 android:layout_height="wrap_content"
                 />
         </TableRow>
    
         <TableRow android:layout_height="wrap_content">
             <TextView
                 android:id="@+id/homeRuns"
                 android:text="0"
                 android:layout_height="wrap_content"
                 />
             <TextView
                 android:id="@+id/homeHits"
                 android:text="0"
                 android:layout_height="wrap_content"
                 />
             <TextView
                 android:id="@+id/homeErrors"
                 android:text="0"
                 android:layout_height="wrap_content"
                 />
         </TableRow>
    </TableLayout>
    

    要在代码中动态更改这些内容,您需要这样:

    // reference the table layout
    TableLayout tbl = (TableLayout)findViewById(R.id.RHE);
    // delcare a new row
    TableRow newRow = new TableRow(this);
    // add views to the row
    newRow.addView(new TextView(this)); // you would actually want to set properties on this before adding it
    // add the row to the table layout
    tbl.addView(newRow);
    

    【讨论】:

    • 是的,我想要一个表格布局视图。列的位置不正确。如何定位柱子?如何动态添加行?
    • 我不确定您所说的“未正确定位”究竟是什么意思。 TableLayout 将根据其属性调整大小,其中的项目也是如此。也许如果您发布了您的 xml 以及您希望看到的内容,我们将有更多的能力来回答这个问题。
    • 我直接使用了xml文件的代码。只需更改前 2 行。即 schemas.android.com/apk/res/android" android:id = "@+id/RHE" 它显示了很多错误。我在这里犯了什么错误?
    • 样式标签会让你失望。我只是从我的一个工作项目中复制了这一部分,我没有注意到它引用了一些 style.xml 属性。我已将它们从示例中删除,如果您现在复制/粘贴它应该可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2013-10-30
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多