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));
}
}

}



更多相关文章

  1. 关于Android(安卓)27.1.1版本不兼容问题 --解决support-annotati
  2. Android(安卓)Studio 新建项目 只有.idea文件夹
  3. android CountDownTimer
  4. android中google“定位查询”编辑
  5. Android(安卓)JS交互与JS代码注入--详解
  6. [转]Android(安卓)Test Auto Test Mu…
  7. android studio怎么在真机中调试程序?
  8. 在Eclipse中查看android 源代码的方法!
  9. 录音函数网络对讲机C#服务器 Android客户端(二) C#服务器代码分析

随机推荐

  1. java中final关键字详解
  2. java 对称加密——密钥与加密后的数据存
  3. 基于james3.0 的邮件系统(struts2.3.2 +sp
  4. 如何在Java中递归解压缩文件?
  5. 试图改变Jtable java中行的颜色
  6. idea下Kotlin的扁平化集合flatMap
  7. 使加权图在JGraphT中工作
  8. 如何将动态参数传递给jquery函数
  9. android中java.net.Socket的默认超时值是
  10. 使用Java管理Azure(1):基础配置