Android使用TableLayou动态布局实例


有图有真相,请先看效果截图:


实现主要功能:

 * 1.使用TableLayout动态布局展示,可动态添加和删除.
 * 2.初始化时显示动态展示,初始化的数据改造后可来自数据库. 
 * 3.重置时到初始化状态.
 * 4.保存时去重检查,参见代码中去重算法.

Android使用TableLayou动态布局实例源码下载地址

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/4月/8日/Android使用TableLayou动态布局实例/

首先,建立实体类:

  1. package tgb.lk.tablelayout;  
  2.   
  3. public class Dict {  
  4.   
  5.     private int id;  
  6.     private String name;  
  7.     private int orders;  
  8.     private String desc;  
  9.     private int types;  
  10.       
  11.     //get,set方法.   
  12.   
  13.     @Override  
  14.     public String toString() {  
  15.         return "Dict [id=" + id + ", name=" + name + ", orders=" + orders  
  16.                 + ", types=" + types + "]";  
  17.     }  
  18.   
  19. }  

其次,建立数据层方法接口:

  1. package tgb.lk.tablelayout;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.content.Context;  
  7.   
  8. public class DictDaoImpl {  
  9.   
  10.     public DictDaoImpl(Context context) {  
  11.   
  12.     }  
  13.   
  14.     public boolean isExist() {  
  15.         return false;  
  16.     }  
  17.   
  18.     public List<Dict> getDictItem(String dictId) {  
  19.         String[] dicts = new String[] { "华东""华南""华北""华中""西南""东北" };  
  20.         List<Dict> retList = new ArrayList<Dict>();  
  21.         for (int j = 0; j < dicts.length; j++) {  
  22.             Dict dict = new Dict();  
  23.             dict.setName(dicts[j]);  
  24.             dict.setId(j);  
  25.             dict.setOrders(j);  
  26.             dict.setTypes(1000);  
  27.             retList.add(dict);  
  28.         }  
  29.         return retList;  
  30.     }  
  31.   
  32.     public long insert(Dict entity) {  
  33.         return 1L;  
  34.     }  
  35.   
  36.     public void delete(int id) {  
  37.     }  
  38.   
  39.     public void update(Dict entity) {  
  40.     }  
  41.   
  42.     public Dict get(Object id) {  
  43.         Dict dict = new Dict();  
  44.         dict.setId(1);  
  45.         dict.setName("华东");  
  46.         dict.setOrders(1);  
  47.         dict.setTypes(1000);  
  48.         return dict;  
  49.     }  
  50.   
  51.     public void delete(Integer... ids) {  
  52.     }  
  53. }  
然后,建立layout布局dict_item.xml:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:scrollbars="none" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/dictLayout"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:orientation="vertical"  
  12.         android:scrollbars="" >  
  13.   
  14.         <TextView  
  15.             android:id="@+id/title"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="wrap_content"  
  18.             android:text="业务字典管理" />  
  19.   
  20.         <LinearLayout  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:orientation="horizontal" >  
  24.   
  25.             <TextView  
  26.                 android:layout_width="80dp"  
  27.                 android:layout_height="wrap_content"  
  28.                 android:text="名称:" />  
  29.   
  30.             <EditText  
  31.                 android:id="@+id/name"  
  32.                 android:layout_width="200dp"  
  33.                 android:layout_height="wrap_content"  
  34.                 android:hint="请输入业务字典名称"  
  35.                 android:maxLength="20"  
  36.                 android:singleLine="true" />  
  37.         </LinearLayout>  
  38.   
  39.         <TableLayout  
  40.             android:id="@+id/dictTable"  
  41.             android:layout_width="fill_parent"  
  42.             android:layout_height="wrap_content"  
  43.             android:stretchColumns="1" >  
  44.   
  45.             <TableRow >  
  46.   
  47.                 <TextView  
  48.                     android:layout_width="60dp"  
  49.                     android:layout_gravity="left"  
  50.                     android:padding="3dip"  
  51.                     android:text="序号"  
  52.                     android:textStyle="bold" />  
  53.   
  54.                 <TextView  
  55.                     android:layout_gravity="center"  
  56.                     android:padding="3dip"  
  57.                     android:text="字典名称"  
  58.                     android:textStyle="bold" />  
  59.   
  60.                 <TextView  
  61.                     android:layout_gravity="right"  
  62.                     android:padding="3dip"  
  63.                     android:text=" 操作 "  
  64.                     android:textStyle="bold" />  
  65.             </TableRow>  
  66.         </TableLayout>  
  67.   
  68.         <LinearLayout  
  69.             android:layout_width="fill_parent"  
  70.             android:layout_height="wrap_content"  
  71.             android:gravity="right"  
  72.             android:orientation="horizontal" >  
  73.   
  74.             <Button  
  75.                 android:id="@+id/btnCancel"  
  76.                 android:layout_width="wrap_content"  
  77.                 android:layout_height="wrap_content"  
  78.                 android:layout_margin="8dp"  
  79.                 android:text=" 关 闭 " />  
  80.   
  81.             <Button  
  82.                 android:id="@+id/btnReset"  
  83.                 android:layout_width="wrap_content"  
  84.                 android:layout_height="wrap_content"  
  85.                 android:layout_margin="8dp"  
  86.                 android:text=" 重 置 " />  
  87.   
  88.             <Button  
  89.                 android:id="@+id/btnAdd"  
  90.                 android:layout_width="wrap_content"  
  91.                 android:layout_height="wrap_content"  
  92.                 android:layout_margin="8dp"  
  93.                 android:text=" 添 加 " />  
  94.   
  95.             <Button  
  96.                 android:id="@+id/btnOK"  
  97.                 android:layout_width="wrap_content"  
  98.                 android:layout_height="wrap_content"  
  99.                 android:layout_margin="8dp"  
  100.                 android:text=" 保 存 " />  
  101.         </LinearLayout>  
  102.     </LinearLayout>  
  103.   
  104. </ScrollView>

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

  • 1
  • 2
  • 下一页

相关内容