TableLayout

来源:互联网 发布:巨人网络 纪学锋 编辑:程序博客网 时间:2024/05/22 00:10

一、基础知识:

 

TableLayout置底,TableRow在TableLayout的上面,而Button、TextView等控件就在TableRow之上,

另外,TableLayout之上也可以单独放控件。TableLayout是一个使用复杂的布局,最简单的用法就仅

仅是拖拉控件做出个界面,但实际上,会经常在代码里使用TableLayout,例如做出表格的效果。

 

android:collapseColumns:以第0行为序,隐藏指定的列
android:shrinkColumns:以第0行为序,自动延伸指定的列填充可用部分
android:stretchColumns:以第0行为序,尽量把指定的列填充空白部分

 

二、方案一代码展示:

方案一采用xml布局,在代码中除了显示layout之外,未作任何布局相关的操作。

1."Acticity_06\src\yan\acticity_06\MainActivity.java"

[java] view plaincopy
  1. package yan.activity_06;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5.   
  6. public class MainActivity extends Activity {  
  7.         
  8.     @Override    
  9.     public void onCreate(Bundle savedInstanceState) {    
  10.         super.onCreate(savedInstanceState);    
  11.         setContentView(R.layout.activity_main);    
  12.     }  
  13. }  

 

2."Activity_06\res\layout\activity_main.xml"

[html] view plaincopy
  1. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     tools:context=".MainActivity"  
  6.     android:stretchColumns="0" >  
  7.  <TableRow>  
  8.         <TextView  
  9.             android:text="第一行第一列"  
  10.             android:background="#aa0000"  
  11.             android:padding="3dip" />  
  12.         <TextView  
  13.             android:text="第一行第二列"  
  14.             android:padding="3dip"  
  15.             android:gravity="center_horizontal"  
  16.             android:background="#00aa00"  
  17.             ></TextView>  
  18.         <TextView  
  19.             android:text="第一行第三列"  
  20.             android:gravity="right"  
  21.             android:background="#0000aa"  
  22.             android:padding="3dip" />  
  23.     </TableRow>  
  24.   
  25.     <TableRow>  
  26.         <TextView  
  27.             android:text="第二行第一列"  
  28.             android:padding="3dip" />  
  29.         <TextView  
  30.             android:text="第二行第二列"  
  31.             android:gravity="right"  
  32.             android:padding="3dip" />  
  33.     </TableRow>  
  34.       
  35.     <TextView  
  36.     android:layout_width="wrap_content"  
  37.     android:layout_height="wrap_content"  
  38.     android:text="@string/hello_world" />  
  39.   
  40. </TableLayout>  


 

三、方案二代码展示:

方案二采用代码布局,在layout文件中除了显示一个空的TabLayout之外,未作任何其它布局。

1."Acticity_06\src\yan\acticity_06\MainActivity.java"

[java] view plaincopy
  1. package yan.activity_06;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.ViewGroup;  
  5. import android.widget.TableLayout;  
  6. import android.widget.TableRow;  
  7. import android.widget.TextView;  
  8. import android.app.Activity;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     /** Called when the activity is first created. */    
  12.     private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;    
  13.     private final int FP = ViewGroup.LayoutParams.FILL_PARENT;    
  14.         
  15.     @Override    
  16.     public void onCreate(Bundle savedInstanceState) {    
  17.         super.onCreate(savedInstanceState);    
  18.         setContentView(R.layout.activity_main);    
  19.         //新建TableLayout01的实例     
  20.         TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);    
  21.         //全部列自动填充空白处     
  22.         tableLayout.setStretchAllColumns(true);    
  23.         //生成10行,8列的表格     
  24.         for(int row=0;row<10;row++)    
  25.         {    
  26.             TableRow tableRow=new TableRow(this);    
  27.             for(int col=0;col<8;col++)    
  28.             {    
  29.                 //tv用于显示     
  30.                 TextView tv=new TextView(this);    
  31.                 tv.setText("("+col+","+row+")");    
  32.                 tableRow.addView(tv);    
  33.             }    
  34.             //新建的TableRow添加到TableLayout     
  35.             tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));    
  36.         }    
  37.     }  
  38. }  


2."Activity_06\res\layout\activity_main.xml"

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     >    
  7.      <TableLayout     
  8.              android:id="@+id/TableLayout01"     
  9.              android:layout_width="fill_parent"     
  10.              android:layout_height="wrap_content">    
  11.      </TableLayout>    
  12. </LinearLayout>    


 


 

 

三、效果展示:

1.方案一:

 

2.方案二:

0 0
原创粉丝点击