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


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


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

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns: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. package com.LayoutDemo;   
    2. import com.LayoutDemo.R;   
    3. import android.app.Activity;   
    4. import android.os.Bundle;   
    5. import android.view.ViewGroup;   
    6. import android.widget.TableLayout;   
    7. import android.widget.TableRow;   
    8. import android.widget.TextView;   
    9. public class LayoutDemo extends Activity {   
    10.     /** Called when the activity is first created. */  
    11.     private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;   
    12.     private final int FP = ViewGroup.LayoutParams.FILL_PARENT;   
    13.        
    14.     @Override  
    15.     public void onCreate(Bundle savedInstanceState) {   
    16.         super.onCreate(savedInstanceState);   
    17.         setContentView(R.layout.main);   
    18.         //新建TableLayout01的实例   
    19.         TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);   
    20.         //全部列自动填充空白处   
    21.         tableLayout.setStretchAllColumns(true);   
    22.         //生成10行,8列的表格   
    23.         for(int row=0;row<10;row++)   
    24.         {   
    25.             TableRow tableRow=new TableRow(this);   
    26.             for(int col=0;col<8;col++)   
    27.             {   
    28.                 //tv用于显示   
    29.                 TextView tv=new TextView(this);   
    30.                 tv.setText("("+col+","+row+")");   
    31.                 tableRow.addView(tv);   
    32.             }   
    33.             //新建的TableRow添加到TableLayout   
    34.             tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));   
    35.         }   
    36.     }   
    37. }  


    结果如下图:


    1

  • 相关阅读:
    【闲暇研究】某问道,问问,道道宠物素材整理
    小米手机刷机,官方教程。
    【闲暇研究】某问道,每日极品道具体验礼包。
    【闲暇研究】某问道,悟道只能用银元宝。
    Windows Server 2016从Evaluation评估版转换成正式版
    【闲暇研究】某问道,仙元、道具合成的修改,与参考文件。
    关于小米手机安装APP总是提示“与已安装应用签名不同”的问题
    WebStorm的快捷键,技巧
    Webstorm(常见配置+Emmet简单语法)
    05-方法重写
  • 原文地址:https://www.cnblogs.com/xyzlmn/p/3168342.html
Copyright © 2020-2023  润新知