Android入門第五篇之TableLayout (二)

來源:互聯網
上載者:User

上一篇文章,主要將如何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"?>
<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);  
                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));
        }
    }
}

 


結果如:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.