`
yidongkaifa
  • 浏览: 4069442 次
文章分类
社区版块
存档分类
最新评论

Android入门第五篇之TableLayout (二) .

 
阅读更多
上一篇文章,主要将如何UI设计器设计TableLayout + TableRow,由于实际应用中,经常需要在代码里往TableLayout添加数据(9宫图也可以用TableLayout做出来 ),本文就是介绍这方面的简单使用方法。


main.xml的代码如下,用到TableLayout的ID为TableLayout01:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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>


JAVA代码如下:

  1. packagecom.LayoutDemo;
  2. importcom.LayoutDemo.R;
  3. importandroid.app.Activity;
  4. importandroid.os.Bundle;
  5. importandroid.view.ViewGroup;
  6. importandroid.widget.TableLayout;
  7. importandroid.widget.TableRow;
  8. importandroid.widget.TextView;
  9. publicclassLayoutDemoextendsActivity{
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. privatefinalintWC=ViewGroup.LayoutParams.WRAP_CONTENT;
  12. privatefinalintFP=ViewGroup.LayoutParams.FILL_PARENT;
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. //新建TableLayout01的实例
  18. TableLayouttableLayout=(TableLayout)findViewById(R.id.TableLayout01);
  19. //全部列自动填充空白处
  20. tableLayout.setStretchAllColumns(true);
  21. //生成10行,8列的表格
  22. for(introw=0;row<10;row++)
  23. {
  24. TableRowtableRow=newTableRow(this);
  25. for(intcol=0;col<8;col++)
  26. {
  27. //tv用于显示
  28. TextViewtv=newTextView(this);
  29. tv.setText("("+col+","+row+")");
  30. tableRow.addView(tv);
  31. }
  32. //新建的TableRow添加到TableLayout
  33. tableLayout.addView(tableRow,newTableLayout.LayoutParams(FP,WC));
  34. }
  35. }
  36. }


结果如下图:


1

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics