歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android使用TableLayou動態布局實例

Android使用TableLayou動態布局實例

日期:2017/3/1 10:25:02   编辑:Linux編程

有圖有真相,請先看效果截圖:


實現主要功能:

* 1.使用TableLayout動態布局展示,可動態添加和刪除.
* 2.初始化時顯示動態展示,初始化的數據改造後可來自數據庫.
* 3.重置時到初始化狀態.
* 4.保存時去重檢查,參見代碼中去重算法.

Android使用TableLayou動態布局實例源碼下載地址

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/4月/8日/Android使用TableLayou動態布局實例/

首先,建立實體類:

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

其次,建立數據層方法接口:

  1. package tgb.lk.tablelayout;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.content.Context;
  5. public class DictDaoImpl {
  6. public DictDaoImpl(Context context) {
  7. }
  8. public boolean isExist() {
  9. return false;
  10. }
  11. public List<Dict> getDictItem(String dictId) {
  12. String[] dicts = new String[] { "華東", "華南", "華北", "華中", "西南", "東北" };
  13. List<Dict> retList = new ArrayList<Dict>();
  14. for (int j = 0; j < dicts.length; j++) {
  15. Dict dict = new Dict();
  16. dict.setName(dicts[j]);
  17. dict.setId(j);
  18. dict.setOrders(j);
  19. dict.setTypes(1000);
  20. retList.add(dict);
  21. }
  22. return retList;
  23. }
  24. public long insert(Dict entity) {
  25. return 1L;
  26. }
  27. public void delete(int id) {
  28. }
  29. public void update(Dict entity) {
  30. }
  31. public Dict get(Object id) {
  32. Dict dict = new Dict();
  33. dict.setId(1);
  34. dict.setName("華東");
  35. dict.setOrders(1);
  36. dict.setTypes(1000);
  37. return dict;
  38. }
  39. public void delete(Integer... ids) {
  40. }
  41. }
然後,建立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. <LinearLayout
  7. android:id="@+id/dictLayout"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:orientation="vertical"
  11. android:scrollbars="" >
  12. <TextView
  13. android:id="@+id/title"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="業務字典管理" />
  17. <LinearLayout
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:orientation="horizontal" >
  21. <TextView
  22. android:layout_width="80dp"
  23. android:layout_height="wrap_content"
  24. android:text="名稱:" />
  25. <EditText
  26. android:id="@+id/name"
  27. android:layout_width="200dp"
  28. android:layout_height="wrap_content"
  29. android:hint="請輸入業務字典名稱"
  30. android:maxLength="20"
  31. android:singleLine="true" />
  32. </LinearLayout>
  33. <TableLayout
  34. android:id="@+id/dictTable"
  35. android:layout_width="fill_parent"
  36. android:layout_height="wrap_content"
  37. android:stretchColumns="1" >
  38. <TableRow >
  39. <TextView
  40. android:layout_width="60dp"
  41. android:layout_gravity="left"
  42. android:padding="3dip"
  43. android:text="序號"
  44. android:textStyle="bold" />
  45. <TextView
  46. android:layout_gravity="center"
  47. android:padding="3dip"
  48. android:text="字典名稱"
  49. android:textStyle="bold" />
  50. <TextView
  51. android:layout_gravity="right"
  52. android:padding="3dip"
  53. android:text=" 操作 "
  54. android:textStyle="bold" />
  55. </TableRow>
  56. </TableLayout>
  57. <LinearLayout
  58. android:layout_width="fill_parent"
  59. android:layout_height="wrap_content"
  60. android:gravity="right"
  61. android:orientation="horizontal" >
  62. <Button
  63. android:id="@+id/btnCancel"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_margin="8dp"
  67. android:text=" 關 閉 " />
  68. <Button
  69. android:id="@+id/btnReset"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:layout_margin="8dp"
  73. android:text=" 重 置 " />
  74. <Button
  75. android:id="@+id/btnAdd"
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:layout_margin="8dp"
  79. android:text=" 添 加 " />
  80. <Button
  81. android:id="@+id/btnOK"
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:layout_margin="8dp"
  85. android:text=" 保 存 " />
  86. </LinearLayout>
  87. </LinearLayout>
  88. </ScrollView>

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved