如何建立在Android中的表具有多个列? [英] How to create a table in Android with multiple columns?
问题描述
我想在Android中使用多列创建一个表。大多数我看到的例子是2列。 (我是新来的Java和Android。)我需要3-4列,我应该能够动态添加表中的行。任何人都可以提供我一个样品code。先谢谢了。(我用eclipse中取胜7)
I want to create a table in android with multiple column. Most of the examples I saw is with 2 columns. (I am new to java and Android.) I need 3-4 columns and I should be able to add the rows dynamically in the table. Can anyone provide me a sample code. Thanks in advance.(I am using eclipse in win 7)
推荐答案
我假设你在谈论在数据库中??
I assume you're talking about a TableLayout view and not a table in a database??
如果是这样,这里有一个表中的三列三行一个XML的例子。
If so, here's an XML example of a table with three columns and three rows.
每个<的TableRow>元素在表中的行,并在元件内的各视图中创建一个塔。我用TextViews,但他们可以ImageViews,EditText上,等等。
Each < TableRow > element creates a row in the table, and each view inside the element creates a "column". I've used TextViews, but they can be ImageViews, EditText, etc.
<?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>
要在code动态地改变这些,你有这样的事情:
To dynamically change these in the code, you'd have something like this:
// 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);
这篇关于如何建立在Android中的表具有多个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!