Android 动态增多控件
Android 动态增加控件
1. package com.fetion.android;
2. 2.
3. 3. import android.app.Activity;
4. 4. import android.content.Context;
5. 5. import android.graphics.Color;
6. 6. import android.os.Bundle;
7. 7. import android.text.Layout;
8. 8. import android.text.format.DateFormat;
9. 9. import android.util.Log;
10. 10. import android.view.KeyEvent;
11. 11. import android.view.ViewGroup.LayoutParams;
12. 12. import android.widget.*;
13. 13.
14. 14. import java.util.Calendar;
15. 15.
16. 16. /**
17. 17. * 测试动态使用android控件
18. 18. * @author gaolei by 20090827
19. 19. */
20. 20. public class fetion2009 extends Activity
21. 21. {
22. 22. /** Called when the activity is first created. */
23. 23. ProgressBar pb; //进度条控件,但拿出来是为了可控,动态改变其进度
24. 24. //聊天对话的底色是间隔的
25. 25. private static final int[] bg = { Color.WHITE, Color.GRAY };
26. 26. private static int bgIndex=0; //聊天对话的底色 当前色应该是bg中的索引值
27. 27.
28. 28. //以下 布局参数 标识当前控件的宽高情况FILL_PARENT=占据全部父控件,WRAP_CONTENT=仅包裹控件中的内容//还有其他作用比如左右边距,这里我们使用默认的
29. 29. private LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
30. 30. private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
31. 31. private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
32. 32.
33. 33. @Override
34. 34. public void onCreate( Bundle savedInstanceState )
35. 35. {
36. 36. super.onCreate( savedInstanceState );
37. 37.
38. 38. //聊天对白窗口需要滚动
39. 39. ScrollView sv = new ScrollView(this);
40. 40. sv.setLayoutParams( LP_FF );
41. 41.
42. 42. LinearLayout layout = new LinearLayout(this); //线性布局方式
43. 43. layout.setOrientation( LinearLayout.VERTICAL ); //控件对其方式为垂直排列
44. 44. layout.setBackgroundColor( 0xff00ffff ); //设置布局板的一个特殊颜色,这可以检验我们会话时候是否有地方颜色不正确!
45. 45.
46. 46. //丰富聊天页面,也顺带测试页面滚动效果,增加了10个重复的对话内容
47. 47. for( int i=0; i<10; i++ )
48. 48. {
49. 49. setSendMsg( layout, this, getCurrColor(), i+"聊天内容在这里。。" );
50. 50. }
51. 51.
52. 52. //发送文件效果1,圆环进度条,也是ProgressBar默认的效果
53. 53. setSendFile( layout, this, getCurrColor(),"我的照片.jpg");
54. 54.
55. 55. //发送文件效果 2,矩行进度条,也是ProgressBar的风格设置成 style="?android:attr/progressBarStyleHorizontal"的效果
56. 56. setSendFile2( layout, this, getCurrColor(),"我的照片.jpg");
57. 57.
58. 58. for( int i=0; i<10; i++ )
59. 59. {
60. 60. setSendMsg( layout, this, getCurrColor(), i+"聊天内容在这里。。" );
61. 61. }
62. 62. sv.addView( layout ); //把线性布局加入到ScrollView中
63. 63. setContentView(sv); //设置当前的页面为ScrollView
64. 64. }
65. 65.
66. 66. /**
67. 67. * 获取当前聊天对白的底色值
68. 68. * @return 当前聊天对白的底色值
69. 69. */
70. 70. private int getCurrColor()
71. 71. {
72. 72. return bg[ (++bgIndex)% bg.length ];
73. 73. }
74. 74.
75. 75. /**
76. 76. * 动态增加一个聊天内容
77. 77. * 这里为了简化编程把 某人说 和 内容放到一个TextView中,可以根据设计文档拆成2个TextView分别显示,设置字体等
78. 78. * @param layout TextView 控件欲添加到的目标layout
79. 79. * @param context 构建View控件的必须参数 既View控件的环境
80. 80. * @param bgColur TextView 控件的背景色
81. 81. * @param MSG TextView 控件要现实的文本内容
82. 82. */
83. 83. private void setSendMsg(LinearLayout layout, Context context, int bgColur, String MSG)
84. 84. {
85. 85. TextView tv = new TextView(context); //普通聊天对话
86. 86. //获取一个全局的日历实例,用于获取当前系统时间并格式化成小时:分钟形式,仅用于测试,这里的时间应该是由其他程序提供
87. 87. tv.setText( "某人 说: ["+DateFormat.format( "kk:mm" , Calendar.getInstance())+"]\n"+MSG );
88. 88. tv.setBackgroundColor( bgColur );
89. 89. layout.addView( tv );
90. 90. }
91. 91.
92. 92. /**
93. 93. * 动态增加一个发送文件的会话条目
94. 94. * 这里因为是发送进度条与取消按钮的水平对其方式,所以需要增加一个LinearLayout
95. 95. * @param layout 欲添加到的目标layout
96. 96. * @param context 构建 View控件的必须参数 既View控件的环境
97. 97. * @param bgColur 控件的背景色
98. 98. * @param MSG 控件要现实的文本内容
99. 99. */
100.100. private void setSendFile(LinearLayout layout, Context context, int bgColur, String fileName)
101.101. {
102.102. //把 某人说 [时间]
103.103. //要发送的文件信息 全都交给 setSendMsg 绘制吧!
104.104. setSendMsg( layout, context, bgColur, "正在发送"+fileName );
105.105. //水平排列2个控件需要一个LinearLayout,排列方式默认的就是水平排列
106.106. LinearLayout myLayout = new LinearLayout(context);
107.107. //这个 LinearLayout控件的背景色需要设置,要不就会显示出主LinearLayout的颜色了,即0xff00ffff
108.108. myLayout.setBackgroundColor( bgColur );
109.109.
110.110. //动态创建一个 ProgressBar,以默认属性加入到myLayout中
111.111. ProgressBar pb = new ProgressBar(context);
112.112. pb.setLayoutParams( LP_WW );
113.113. myLayout.addView( pb );
114.114.
115.115. //动态创建一个 Button,以默认属性加入到myLayout中
116.116. Button bt = new Button(context);
117.117. bt.setLayoutParams( LP_WW );
118.118. bt.setText( " 取消" );
119.119. myLayout.addView( bt );
120.120. //将水平布局的 LinearLayout及其内如所有控件添加到主layout中
121.121. layout.addView( myLayout );
122.122. }
123.123.
124.124. /**
125.125. * 动态增加一个发送文件的会话条目
126.126. * 但为了保障ProgressBar和 Button的底色符合设计要求,增加了一个LinearLayout,并设置其背景色
127.127. * @param layout 欲添加到的目标layout
128.128. * @param context 构建 View控件的必须参数 既View控件的环境
129.129. * @param bgColur 控件的背景色
130.130. * @param MSG 控件要现实的文本内容
131.131. */
132.132. private void setSendFile2(LinearLayout layout, Context context, int bgColur, String fileName)
133.133. {
134.134. setSendMsg( layout, context, bgColur, "正在发送"+fileName );
135.135.
136.136. LinearLayout myLayout = new LinearLayout(context);
137.137. myLayout.setBackgroundColor( bgColur );
138.138. myLayout.setOrientation( LinearLayout.VERTICAL );//控件对其方式为垂直,默认为水平
139.139.
140.140. //ProgressBar 的默认风格是圆环型,这里需要设置她的风格为Horizontal(水平线)
141.141. pb = new ProgressBar(context,null,android.R.attr.progressBarStyleHorizontal);
142.142. pb.setLayoutParams( LP_FW );
143.143. pb.setProgress( 45 ); // 设置第1进度为45
144.144. pb.setSecondaryProgress( 0 ); //这里我们不需要第2进度,所以为0
145.145. myLayout.addView( pb );
146.146.
147.147. Button bt = new Button(context);
148.148. bt.setLayoutParams( LP_WW );
149.149. bt.setText( "取消" );
150.150. myLayout.addView( bt );
151.151.
152.152. layout.addView( myLayout );
153.153. }
154.154.
155.155. @Override
156.156. public boolean onKeyDown(int keyCode, KeyEvent event)
157.157. {
158.158. Log.d("onKeyDown:", " keyCode=" + keyCode + " KeyEvent=" + event);
159.159. switch (keyCode)
160.160. {
161.161. case KeyEvent.KEYCODE_DPAD_UP:
162.162.
163.163. break;
164.164. case KeyEvent.KEYCODE_DPAD_DOWN:
165.165.
166.166. break;
167.167. case KeyEvent.KEYCODE_DPAD_LEFT:
168.168. //右左按键可以控制第一进度的增减
169.169. pb.setProgress( pb.getProgress()-5 );
170.170. break;
171.171. case KeyEvent.KEYCODE_DPAD_RIGHT:
172.172. pb.setProgress( pb.getProgress()+5 );
173.173. break;
174.174. case KeyEvent.KEYCODE_DPAD_CENTER:
175.175.
176.176. break;
177.177. case KeyEvent.KEYCODE_0:
178.178. break;
179.179. }
180.180. return super.onKeyDown(keyCode, event);
181.181. }
182.182. }
1. package com.fetion.android;
2. 2.
3. 3. import android.app.Activity;
4. 4. import android.content.Context;
5. 5. import android.graphics.Color;
6. 6. import android.os.Bundle;
7. 7. import android.text.Layout;
8. 8. import android.text.format.DateFormat;
9. 9. import android.util.Log;
10. 10. import android.view.KeyEvent;
11. 11. import android.view.ViewGroup.LayoutParams;
12. 12. import android.widget.*;
13. 13.
14. 14. import java.util.Calendar;
15. 15.
16. 16. /**
17. 17. * 测试动态使用android控件
18. 18. * @author gaolei by 20090827
19. 19. */
20. 20. public class fetion2009 extends Activity
21. 21. {
22. 22. /** Called when the activity is first created. */
23. 23. ProgressBar pb; //进度条控件,但拿出来是为了可控,动态改变其进度
24. 24. //聊天对话的底色是间隔的
25. 25. private static final int[] bg = { Color.WHITE, Color.GRAY };
26. 26. private static int bgIndex=0; //聊天对话的底色 当前色应该是bg中的索引值
27. 27.
28. 28. //以下 布局参数 标识当前控件的宽高情况FILL_PARENT=占据全部父控件,WRAP_CONTENT=仅包裹控件中的内容//还有其他作用比如左右边距,这里我们使用默认的
29. 29. private LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
30. 30. private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
31. 31. private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
32. 32.
33. 33. @Override
34. 34. public void onCreate( Bundle savedInstanceState )
35. 35. {
36. 36. super.onCreate( savedInstanceState );
37. 37.
38. 38. //聊天对白窗口需要滚动
39. 39. ScrollView sv = new ScrollView(this);
40. 40. sv.setLayoutParams( LP_FF );
41. 41.
42. 42. LinearLayout layout = new LinearLayout(this); //线性布局方式
43. 43. layout.setOrientation( LinearLayout.VERTICAL ); //控件对其方式为垂直排列
44. 44. layout.setBackgroundColor( 0xff00ffff ); //设置布局板的一个特殊颜色,这可以检验我们会话时候是否有地方颜色不正确!
45. 45.
46. 46. //丰富聊天页面,也顺带测试页面滚动效果,增加了10个重复的对话内容
47. 47. for( int i=0; i<10; i++ )
48. 48. {
49. 49. setSendMsg( layout, this, getCurrColor(), i+"聊天内容在这里。。" );
50. 50. }
51. 51.
52. 52. //发送文件效果1,圆环进度条,也是ProgressBar默认的效果
53. 53. setSendFile( layout, this, getCurrColor(),"我的照片.jpg");
54. 54.
55. 55. //发送文件效果 2,矩行进度条,也是ProgressBar的风格设置成 style="?android:attr/progressBarStyleHorizontal"的效果
56. 56. setSendFile2( layout, this, getCurrColor(),"我的照片.jpg");
57. 57.
58. 58. for( int i=0; i<10; i++ )
59. 59. {
60. 60. setSendMsg( layout, this, getCurrColor(), i+"聊天内容在这里。。" );
61. 61. }
62. 62. sv.addView( layout ); //把线性布局加入到ScrollView中
63. 63. setContentView(sv); //设置当前的页面为ScrollView
64. 64. }
65. 65.
66. 66. /**
67. 67. * 获取当前聊天对白的底色值
68. 68. * @return 当前聊天对白的底色值
69. 69. */
70. 70. private int getCurrColor()
71. 71. {
72. 72. return bg[ (++bgIndex)% bg.length ];
73. 73. }
74. 74.
75. 75. /**
76. 76. * 动态增加一个聊天内容
77. 77. * 这里为了简化编程把 某人说 和 内容放到一个TextView中,可以根据设计文档拆成2个TextView分别显示,设置字体等
78. 78. * @param layout TextView 控件欲添加到的目标layout
79. 79. * @param context 构建View控件的必须参数 既View控件的环境
80. 80. * @param bgColur TextView 控件的背景色
81. 81. * @param MSG TextView 控件要现实的文本内容
82. 82. */
83. 83. private void setSendMsg(LinearLayout layout, Context context, int bgColur, String MSG)
84. 84. {
85. 85. TextView tv = new TextView(context); //普通聊天对话
86. 86. //获取一个全局的日历实例,用于获取当前系统时间并格式化成小时:分钟形式,仅用于测试,这里的时间应该是由其他程序提供
87. 87. tv.setText( "某人 说: ["+DateFormat.format( "kk:mm" , Calendar.getInstance())+"]\n"+MSG );
88. 88. tv.setBackgroundColor( bgColur );
89. 89. layout.addView( tv );
90. 90. }
91. 91.
92. 92. /**
93. 93. * 动态增加一个发送文件的会话条目
94. 94. * 这里因为是发送进度条与取消按钮的水平对其方式,所以需要增加一个LinearLayout
95. 95. * @param layout 欲添加到的目标layout
96. 96. * @param context 构建 View控件的必须参数 既View控件的环境
97. 97. * @param bgColur 控件的背景色
98. 98. * @param MSG 控件要现实的文本内容
99. 99. */
100.100. private void setSendFile(LinearLayout layout, Context context, int bgColur, String fileName)
101.101. {
102.102. //把 某人说 [时间]
103.103. //要发送的文件信息 全都交给 setSendMsg 绘制吧!
104.104. setSendMsg( layout, context, bgColur, "正在发送"+fileName );
105.105. //水平排列2个控件需要一个LinearLayout,排列方式默认的就是水平排列
106.106. LinearLayout myLayout = new LinearLayout(context);
107.107. //这个 LinearLayout控件的背景色需要设置,要不就会显示出主LinearLayout的颜色了,即0xff00ffff
108.108. myLayout.setBackgroundColor( bgColur );
109.109.
110.110. //动态创建一个 ProgressBar,以默认属性加入到myLayout中
111.111. ProgressBar pb = new ProgressBar(context);
112.112. pb.setLayoutParams( LP_WW );
113.113. myLayout.addView( pb );
114.114.
115.115. //动态创建一个 Button,以默认属性加入到myLayout中
116.116. Button bt = new Button(context);
117.117. bt.setLayoutParams( LP_WW );
118.118. bt.setText( " 取消" );
119.119. myLayout.addView( bt );
120.120. //将水平布局的 LinearLayout及其内如所有控件添加到主layout中
121.121. layout.addView( myLayout );
122.122. }
123.123.
124.124. /**
125.125. * 动态增加一个发送文件的会话条目
126.126. * 但为了保障ProgressBar和 Button的底色符合设计要求,增加了一个LinearLayout,并设置其背景色
127.127. * @param layout 欲添加到的目标layout
128.128. * @param context 构建 View控件的必须参数 既View控件的环境
129.129. * @param bgColur 控件的背景色
130.130. * @param MSG 控件要现实的文本内容
131.131. */
132.132. private void setSendFile2(LinearLayout layout, Context context, int bgColur, String fileName)
133.133. {
134.134. setSendMsg( layout, context, bgColur, "正在发送"+fileName );
135.135.
136.136. LinearLayout myLayout = new LinearLayout(context);
137.137. myLayout.setBackgroundColor( bgColur );
138.138. myLayout.setOrientation( LinearLayout.VERTICAL );//控件对其方式为垂直,默认为水平
139.139.
140.140. //ProgressBar 的默认风格是圆环型,这里需要设置她的风格为Horizontal(水平线)
141.141. pb = new ProgressBar(context,null,android.R.attr.progressBarStyleHorizontal);
142.142. pb.setLayoutParams( LP_FW );
143.143. pb.setProgress( 45 ); // 设置第1进度为45
144.144. pb.setSecondaryProgress( 0 ); //这里我们不需要第2进度,所以为0
145.145. myLayout.addView( pb );
146.146.
147.147. Button bt = new Button(context);
148.148. bt.setLayoutParams( LP_WW );
149.149. bt.setText( "取消" );
150.150. myLayout.addView( bt );
151.151.
152.152. layout.addView( myLayout );
153.153. }
154.154.
155.155. @Override
156.156. public boolean onKeyDown(int keyCode, KeyEvent event)
157.157. {
158.158. Log.d("onKeyDown:", " keyCode=" + keyCode + " KeyEvent=" + event);
159.159. switch (keyCode)
160.160. {
161.161. case KeyEvent.KEYCODE_DPAD_UP:
162.162.
163.163. break;
164.164. case KeyEvent.KEYCODE_DPAD_DOWN:
165.165.
166.166. break;
167.167. case KeyEvent.KEYCODE_DPAD_LEFT:
168.168. //右左按键可以控制第一进度的增减
169.169. pb.setProgress( pb.getProgress()-5 );
170.170. break;
171.171. case KeyEvent.KEYCODE_DPAD_RIGHT:
172.172. pb.setProgress( pb.getProgress()+5 );
173.173. break;
174.174. case KeyEvent.KEYCODE_DPAD_CENTER:
175.175.
176.176. break;
177.177. case KeyEvent.KEYCODE_0:
178.178. break;
179.179. }
180.180. return super.onKeyDown(keyCode, event);
181.181. }
182.182. }