TableLayout 动态表格

    Android的TableLayout + TableRow虽然使用有点复杂,但是功能很强大。。。。。。 Android
提供了很多布局属性,但是手机程序的界面没有 PC 那么花俏,所以常用的就那几项而已。。。  
 
TableLayout (二)   
 
      TableLayout (一)主要将如何UI设计器设计TableLayout + TableRow,由于实际应用中,
经常需要在代码里往TableLayout添加数据(9 宫图也可以用TableLayout做出来 ),本文就是介
绍这方面的简单使用方法。  
 
 
main.xml的代码如下,用到TableLayout的ID为TableLayout01:  
view plaincopy to clipboardprint? 
<?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">   
      </TableLayout>   
</LinearLayout>   
<?xml version="1.0" encoding="utf-8"?> Android开发文档    2010 




<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"> 
      </TableLayout> 
</LinearLayout>  
 
 
JAVA代码如下:  
view plaincopy to clipboardprint? 
package com.LayoutDemo;     
import com.LayoutDemo.R;     
import android.app.Activity;     
import android.os.Bundle;     
import android.view.ViewGroup;     
import android.widget.TableLayout;     
import android.widget.TableRow;     
import android.widget.TextView;     
public class LayoutDemo extends Activity {     
      /** Called when the activity is first created. */   
      private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;     
      private final int FP = ViewGroup.LayoutParams.FILL_PARENT;     
        
      @Override   
      public void onCreate(Bundle savedInstanceState) {     
          super.onCreate(savedInstanceState);     
          setContentView(R.layout.main);     
          //新建TableLayout01的实例     
          TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);     
          //全部列自动填充空白处     
          tableLayout.setStretchAllColumns(true);     
          //生成 10 行,8 列的表格     
          for(int row=0;row<10;row++)     
          {     
                TableRow tableRow=new TableRow(this);     
                for(int col=0;col<8;col++)     
                {     
                //tv用于显示     
                      TextView tv=new TextView(this);     Android开发文档    2010 
作者: craining   (曲阜师范大学)  个人主页: http://craining.blog.163.com/  邮箱: craining@163.com  44
                tv.setText("("+col+","+row+")");     
                tableRow.addView(tv);    
                }     
                //新建的TableRow添加到TableLayout     
                tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));     
          }     
      }     
}   
package com.LayoutDemo; 
import com.LayoutDemo.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.ViewGroup; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
public class LayoutDemo extends Activity { 
      /** Called when the activity is first created. */ 
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 
private final int FP = ViewGroup.LayoutParams.FILL_PARENT; 
 
    @Override 
      public void onCreate(Bundle savedInstanceState) { 
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.main); 
          //新建TableLayout01的实例 
          TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01); 
          //全部列自动填充空白处 
          tableLayout.setStretchAllColumns(true); 
          //生成 10 行,8 列的表格 
          for(int row=0;row<10;row++) 
          { 
                TableRow tableRow=new TableRow(this); 
                for(int col=0;col<8;col++) 
                { 
             //tv用于显示 
             TextView tv=new TextView(this); 
                tv.setText("("+col+","+row+")"); 
                tableRow.addView(tv); 
                } 
                //新建的TableRow添加到TableLayout 
                tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC)); 
          } 
    }

}



posted @ 2012-10-30 13:34  sfshine  阅读(340)  评论(0编辑  收藏  举报