Android入门之TableLayout应用解析(二)

Android入门之TableLayout应用解析(二),第1张

概述本文在上一篇初步介绍TableLayout常用属性的基础上,将进一步介绍如何UI设计器设计TableLayout+TableRow。由于实际应用中,经常需要在代码里往TableLayout添加数据(9宫图也可以用TableLayout做出来),本文就是介绍这

本文在上一篇初步介绍tableLayout常用属性的基础上,将进一步介绍如何UI设计器设计tableLayout + tableRow。由于实际应用中,经常需要在代码里往tableLayout添加数据(9宫图也可以用tableLayout做出来 ),本文就是介绍这方面的简单使用方法。

main.xml的代码如下,用到tableLayout的ID为tableLayout01:

<?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代码部分如下:

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

结果如下图:

总结

以上是内存溢出为你收集整理的Android入门之TableLayout应用解析(二)全部内容,希望文章能够帮你解决Android入门之TableLayout应用解析(二)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存