展会信息港展会大全

Android入门第五篇之TableLayout (二)
来源:互联网   发布日期:2015-11-15 10:00:21   浏览:1792次  

导读:上一篇文章,主要将如何UI设计器设计TableLayout + TableRow,由于实际应用中,经常需要在代码里往TableLayout添加数据(9宫图也可以用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"><TableLayoutandroid: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"><TableLayoutandroid: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;@Overridepublic 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添加到TableLayouttableLayout.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;@Overridepublic 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添加到TableLayouttableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));}}}

结果如下图:

赞助本站

人工智能实验室

相关热词: android开发 教程

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港