`
yezhiqiu-love
  • 浏览: 168668 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

动态增加TableLayout的行

阅读更多

Just like HTML Tables on webpages the TableLayout on Android gives you the option to align Views in a table order with rows and columns.

My development setup is:

IDE: Eclipse IDE (setup guide here ).
Phone: HTC Hero
Android version: 1.5 HTC Rom

I will be using the standard Android Project Skeleton in Eclipse as my base setup. This auto creates all the basic files needed, so these will not be covered here.

TableLayout on Android

The TableLayout is built using the TableLayout and the TableRow commands. There is no TableCols like the <td> tag in HTML. To align your view in columns you have to set the width of the elements and manually control the layout.

XML Layout

To make a basic layout with 2 rows and 4 textview I have a main.xml with this content:

< TableLayout android:id = "@+id/TableLayout01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
 
   < TableRow android:id = "@+id/TableRow01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
 
     < TextView android:id = "@+id/TextView01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "textfield 1-1" ></ TextView >
     < TextView android:id = "@+id/TextView02" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "textfield 1-2" ></ TextView >
 
   </ TableRow >
 
   < TableRow android:id = "@+id/TableRow02" android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
 
     < TextView android:id = "@+id/TextView03" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "textfield 2-1" ></ TextView >
     < TextView android:id = "@+id/TextView04" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "textfield 2-2" ></ TextView >
 
   </ TableRow >
 
</ TableLayout >

This creates a layout like this:
Android TableLayout 1

So we have now displayed a simple TableLayout.

Right aligning checkboxes

Many apps have a nice clean setup where the checkboxes is right aligned to the screen and this is actually very easy to achieve. You just have to set the width of the textview so it will push the checkbox to the right.

First step is to change the TextView02 to a Checkbox like this:

...
     < TextView android:id = "@+id/TextView01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "textfield 1-1" ></ TextView >
     < CheckBox android:id = "@+id/CheckBox01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" ></ CheckBox >

This will output somethings like this:
Android TableLayout 2

Now we just have to align the checkbox.

We can do this by setting the width of the previous textfield like this:

< TableRow android:id = "@+id/TableRow01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" >< TextView android:id = "@+id/TextView01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "textfield 1-1" android:width = "250px" ></ TextView >

Notice the width=”250px” in the end there. This results in:
Android TableLayout 3

But what is the screen size changes? The checkbox will then be placed 250px from the left because the width is hardcoded. Not that great – but yet very easy to adjust.

On the LayoutTable we can adjust stretching across columns, similar to the colspan tag in HTML.

...
< TableLayout android:id = "@+id/TableLayout01" android:layout_width = "fill_parent" android:layout_height = "wrap_content"   android:stretchColumns = "0" >
...

This will make the first (“0″) column stretch as most as needed and allowed by the other elements.:
Android TableLayout 4

And with the screen flipped:
Android TableLayout 5

Complete XML layout:

<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android "
     android:orientation = "vertical"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent"
     >
 
< TableLayout android:id = "@+id/TableLayout01" android:layout_width = "fill_parent" android:layout_height = "wrap_content"   android:stretchColumns = "0" >
 
   < TableRow android:id = "@+id/TableRow01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
     < TextView android:id = "@+id/TextView01" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "textfield 1-1" ></ TextView >
 
     < CheckBox android:id = "@+id/CheckBox01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" ></ CheckBox >
   </ TableRow >
 
   < TableRow android:id = "@+id/TableRow02" android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
     < TextView android:id = "@+id/TextView03" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "textfield 2-1" ></ TextView >
     < TextView android:id = "@+id/TextView04" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "textfield 2-2" ></ TextView >
 
   </ TableRow >
 
</ TableLayout >
</ LinearLayout >

Appending rows dynamically

Lets say you want to add rows to the TableLayout on depend during your app. No problem.

Add a button at the top of your application like this:

< Button android:id = "@+id/Button01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Add row" ></ Button >
 
< ScrollView android:id = "@+id/ScrollView01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
 
   < TableLayout android:id = "@+id/TableLayout01" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:stretchColumns = "0" >
 
     < TableRow android:id = "@+id/TableRow01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
       < TextView android:id = "@+id/TextView01" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "textfield 1-1" ></ TextView >
 
       < CheckBox android:id = "@+id/CheckBox01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" ></ CheckBox >
     </ TableRow >
 
   </ TableLayout >
</ ScrollView >

There is now a button available for handling clicks from the user and the TableLayout has been wrapped in a ScrollView which enables the scroll functionality.

Now for some Java code

package huuah.tablelayout;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TextView;
import android.view.View;
 
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
 
public class tablelayout extends Activity implements OnClickListener {
     /** Called when the activity is first created. */
 
     //initialize a button and a counter
     Button btn;
     int counter = 0 ;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
 
         // setup the layout
         setContentView(R.layout.main);
 
         // add a click-listener on the button
         btn = (Button) findViewById(R.id.Button01);
         btn.setOnClickListener( this );       
 
     }
 
     // run when the button is clicked
     public void onClick(View view) {
 
         // get a reference for the TableLayout
         TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
 
         // create a new TableRow
         TableRow row = new TableRow( this );
 
         // count the counter up by one
         counter++;
 
         // create a new TextView
         TextView t = new TextView( this );
         // set the text to "text xx"
         t.setText( "text " + counter);
 
         // create a CheckBox
         CheckBox c = new CheckBox( this );
 
         // add the TextView and the CheckBox to the new TableRow
         row.addView(t);
         row.addView(c);
 
         // add the TableRow to the TableLayout
         table.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
 
     }
}

Android TableLayout 6

So whenever the button is clicked, it adds a new row to the TableLayout and notice the Scrollbar from the ScrollView.

If you want more information and TableLayout and placement techniques I can recommend reading:

Thats it for now. Enjoy coding and please post some comments.

原文:http://huuah.com/using-tablelayout-on-android/

分享到:
评论

相关推荐

    c# tablelayoutpanel 动态增加和删除列 已测试通过 动态合并单元格 动态添加控件

    首先,让我们详细讨论如何动态增加和删除列。`TableLayoutPanel`默认创建时可能只有预设的列数,但可以通过编程方式动态调整。增加列可以使用`Columns`集合的`Add`方法,例如: ```csharp TableLayoutPanel ...

    TableLayout背景

    - 为了性能优化,避免在TableLayout中使用过多的View,尤其是复杂的View,因为这会导致布局解析和绘制时间增加。 - 在表格的列宽设置上要谨慎,合理使用`android:stretchColumns`,以确保在不同设备和屏幕尺寸上的...

    TableLayout.jar

    `TableLayout.jar`是一个强大的布局管理器,它为开发者提供了一种灵活且易于使用的工具,能够实现与`GridBagLayout`相同的功能,但使用起来却更为简洁,几乎不增加额外的学习难度,相比`GridLayout`,`TableLayout`...

    Android移动应用开发表格布局TableLayout的常用属性.pdf

    需要注意的是,虽然TableLayout提供了强大的布局能力,但在处理动态数据或大量数据时,使用ListView或RecyclerView可能会更高效,因为它们有更好的滚动性能和内存管理。然而,在需要简单表格布局的场景下,...

    design tablelayout demo

    1. **伸缩行和列(Stretchable Rows and Columns)**:TableLayout可以自动拉伸行或列来填充剩余的空间,这通过`android:stretchColumns`和`android:shrinkColumns`属性实现。当设置某个列为可伸展时,该列将在空间...

    android动态添加表格行-IT计算机-毕业设计.zip

    这样可以在运行时根据需要动态地增加表格行。例如: ```java TableRow row = new TableRow(context); TextView textView = new TextView(context); textView.setText("新行数据"); row.addView(textView); ...

    智能家居系统 表格布局TableLayout.doc

    - 嵌套支持:TableLayout可以嵌套在其他布局中,如LinearLayout或RelativeLayout,增加布局的复杂性和灵活性。 2. **表格布局的属性**: - `android:stretchColumns`:指定哪些列应该拉伸以填充可用空间。默认...

    动态添加表格行.zip

    动态添加行意味着我们需要在运行时创建`TableRow`对象,并将其添加到`TableLayout`中。这通常在Java代码中完成,而不是在XML布局文件中预定义。 2. **创建`TableRow`**:`TableRow`是`LinearLayout`的子类,所以它...

    Android中使用TableLayout布局设计办公电话一览表的代码清单.pdf

    TableLayout可以容纳更多的`TableRow`,每行可以添加任意数量的`View`,如`TextView`、`ImageView`或自定义控件。通过调整`TextView`的属性,可以实现不同样式的单元格。例如,可以使用`android:textStyle`设置文本...

    Android中使用TableLayout布局设计办公电话一览表教学案例要求说明.pdf

    - 在XML布局文件中,使用`&lt;TableLayout&gt;`标签开始布局,然后添加`&lt;TableRow&gt;`作为行,接着在每行内添加3个`&lt;TextView&gt;`。 - 使用属性如`android:textSize`、`android:textColor`、`android:background`、`android:...

    TableLayout.zip

    - **可折叠/展开的行**:通过增加状态管理和动画效果,实现类似树形结构的表格。 - **多选和排序功能**:添加选择框和手势操作,允许用户进行多行选择和列排序。 - **自适应列宽**:根据单元格内容自动调整列宽,以...

    Android 表格控件-动态实现表格效果(内容、样式可扩展)

    创建一个简单的动态表格,首先在XML布局文件中添加TableLayout,然后在Activity中创建并添加TableRow,每行中添加TextView。例如: ```java TableLayout table = findViewById(R.id.table); for (int i = 0; i ;...

    TableLayout:Ejercicio de datos dinamicos摆在桌子上

    TableLayout是LinearLayout的一个子类,继承了其线性排列的特性,但增加了表格布局的能力。在Android中,TableLayout由多个TableRow组成,每个TableRow可以包含多个View,这些View相当于表格中的单元格。通过调整...

    Android开发之动态生成表格及其边框

    ### Android开发之动态生成表格及其边框 #### 一、引言 在Android应用开发过程中,经常需要根据...此外,还可以进一步扩展此功能,比如增加对单元格数据的动态更新、支持滚动条等高级特性,以满足更复杂的业务场景。

    android table

    - TableLayout由一个或多个TableRow组成,每个TableRow代表表格的一行。 - 表格中的列宽会根据其内容自动调整,但也可以通过设置权重(weight)来分配固定宽度。 - 默认情况下,TableLayout会尽可能填充其父视图...

    TableLayoutTest

    3. **Span属性**:TableLayout中的View可以通过设置`android:layout_span`属性跨多列显示,增加界面的灵活性。 4. **权重(weight)分配**:在TableRow中,可以为View分配权重,以便根据权重比例自动调整大小,尤其...

    应用源码之MyTableLayout.zip

    3. **动态添加与删除单元格**:MyTableLayout通常会支持在运行时动态增加或删除单元格,这就需要实现添加、删除View的方法,并确保布局更新的正确性。 4. **事件处理**:自定义布局可能需要处理各种触摸事件,例如...

    android ListView实现表格

    - 添加行:在Adapter中增加一个方法,比如`addRow(List&lt;String&gt; rowData)`,接收一行数据,更新Adapter的数据源并调用`notifyDataSetChanged()`通知ListView刷新。 - 添加列:可以考虑创建一个二维数组或者自定义...

    安卓Android源码——MyTableLayout.rar

    TableLayout支持动态添加行和列,并且可以根据内容自动调整大小。 在创建MyTableLayout的过程中,开发者可能面临了一些挑战,比如: 1. **性能优化**:原生TableLayout在处理大量数据时可能会出现性能瓶颈,尤其是...

    Android代码-ExpandTable

    `TableLayout`是Android提供的一个视图布局,它允许开发者创建类似电子表格的布局,由多个行(`TableRow`)组成,每行可以包含多个列。在`TableLayout`中,每个`TableRow`都是一行,而`TableRow`内部可以添加各种...

Global site tag (gtag.js) - Google Analytics