首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android中创建多列的表?

如何在Android中创建多列的表?
EN

Stack Overflow用户
提问于 2011-03-08 13:55:53
回答 1查看 67.6K关注 0票数 17

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-08 14:04:21

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

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

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

代码语言:javascript
运行
复制
<?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>

要在代码中动态更改这些参数,您可以使用如下代码:

代码语言:javascript
运行
复制
// 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);
票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5228898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档