上一篇文章,主要将如何UI设计器设计TableLayout + TableRow,由于实际应用中,经常需要在代码里往TableLayout添加数据(9宫图也可以用TableLayout做出来 ),本文就是介绍这方面的简单使用方法。
main.xml的代码如下,用到TableLayout的ID为TableLayout01:
- <?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns: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代码如下:
- packagecom.LayoutDemo;
-
importcom.LayoutDemo.R;
-
importandroid.app.Activity;
-
importandroid.os.Bundle;
-
importandroid.view.ViewGroup;
-
importandroid.widget.TableLayout;
-
importandroid.widget.TableRow;
-
importandroid.widget.TextView;
-
publicclassLayoutDemoextendsActivity{
-
-
privatefinalintWC=ViewGroup.LayoutParams.WRAP_CONTENT;
-
privatefinalintFP=ViewGroup.LayoutParams.FILL_PARENT;
-
@Override
-
publicvoidonCreate(BundlesavedInstanceState){
-
super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- TableLayouttableLayout=(TableLayout)findViewById(R.id.TableLayout01);
-
-
tableLayout.setStretchAllColumns(true);
-
-
for(introw=0;row<10;row++)
- {
-
TableRowtableRow=newTableRow(this);
-
for(intcol=0;col<8;col++)
- {
-
-
TextViewtv=newTextView(this);
-
tv.setText("("+col+","+row+")");
- tableRow.addView(tv);
- }
-
-
tableLayout.addView(tableRow,newTableLayout.LayoutParams(FP,WC));
- }
- }
- }
结果如下图:
分享到:
相关推荐
Android入门第五篇之TableLayout (二).docx
Android入门第四篇之TableLayout (一).docx
Android应用开发
Android移动应用开发表格布局TableLayout的特点.pdf 学习资料 复习资料 教学资源
最小包,不含example
19Android-05-tableLayout.mp4
Android移动应用开发表格布局TableLayout的常用属性.pdf 学习资料 复习资料 教学资源
android 利用TableLayout自动生成表格
Android使用TableLayout动态布局实例: http://blog.csdn.net/lk_blog/article/details/7436380
第5章 Android中的视图组件 5.1 视图组件 5.1.1 图片视图(ImageView) 5.1.2 滚动视图(ScrollView) 5.1.3 网格视图(GridView) 5.1.4 列表视图(ListView) 5.1.5 切换图片(ImageSwitcher和Gallery) 5.1.6 ...
程序源代码下载 博文链接:https://greatwqs.iteye.com/blog/1045709
NULL 博文链接:https://hanllove001.iteye.com/blog/1185546
本文初步讲述了Android中TableLayout的应用,对Android初学者有一定的学习借鉴价值。具体如下: TableLayout跟TableLayout 是一组搭配使用的布局,TableLayout置底,TableRow在TableLayout的上面,而Button、TextView...
NULL 博文链接:https://xiongxu.iteye.com/blog/797189
主要介绍了Android入门之TableLayout应用,需要的朋友可以参考下
TableLayout,实现动态添加和删除行,判断行中的数据是否含有空值,可以手动地输入数据,也可以选择数据添加在表格中,并实现统计表格中的数据。
NULL 博文链接:https://yangguangfu.iteye.com/blog/678977
智能家居系统 表格布局TableLayout.doc 学习资料 复习资料 教学资源
Android-TableLayout-Example Android-TableLayout-Example