Dynamically add controls for Android)

Source: Internet
Author: User

Core Tip: Android controls are dynamically used. Generally, the interface layout in Android is set in XML, that isProgram, You cannot change the number of elements on the interface, such

Dynamic use of Android controls

Generally, the interface layout in Android is set in XML.
That is to say
In a program, you cannot change the number of elements on the interface,
For example, a chat session Interface
When someone speaks, A textview will be added,
This is the dynamic addition of controls,
This cannot be preconfigured in XML!
But fortunately, Android does not only use XML to use controls.
The following code is a Java program for dynamically producing controls:
The implementation result is as follows:

JavaCode

1. Package Com. fetion. Android; 2. 3. Import Android. App. Activity; 4. Import Android. content. Context; 5. Import Android. Graphics. color; 6. Import Android. OS. Bundle; 7. Import Android. Text. layout; 8. Import Android. Text. format. dateformat; 9. Import Android. util. log; 10. Import Android. View. keyevent; 11. Import Android. View. viewgroup. layoutparams; 12. Import Android. widget. *; 13. 14. Import Java. util. calendar; 15. 16. /** 17. * test the dynamic use of the android control 18. * @ author gaolei by 20090827 19 .*/ 20. Public  Class Fetion2009 Extends Activity 21. {22. /** Called when the activity is first created .*/ 23. progressbar Pb; // Progress bar control, but it is used to control and dynamically change its progress 24. // The background color of the chat conversation is separated. 25. Private   Static   Final   Int [] BG = {color. White, color. Gray}; 26. Private   Static   Int Bgindex = 0; // The background color of the chat conversation. The current color should be the index value in BG. 27. 28. // The following layout parameter identifies the width and height of the current control. fill_parent = occupies all parent controls. wrap_content = only wraps the content in the control. // There are other functions such as left and right margins. Here we use the default 29. Private Linearlayout. layoutparams lp_ff = New Linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); 30. Private Linearlayout. layoutparams lp_fw = New Linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); 31. Private Linearlayout. layoutparams lp_ww = New Linearlayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); 32. 33. @ override 34. Public   Void Oncreate (bundle savedinstancestate) 35. {36. Super . Oncreate (savedinstancestate); 37. 38. // Scroll through the chat dialog box 39. scrollview SV = New Scrollview ( This ); 40. sv. setlayoutparams (lp_ff); 41. 42. linearlayout = New Linearlayout ( This );// Linear Layout 43. layout. setorientation (linearlayout. Vertical ); // The control is vertically arranged. 44. layout. setbackgroundcolor (0xff00ffff ); // Set a special color for the layout board, which can check whether there is any incorrect color during the session! 45. 46. // Enrich the chat page and test the Page scrolling effect, adding 10 repeated conversations 47. For ( Int I = 0; I <10; I ++) 48. {49. setsendmsg (layout, This , Getcurrcolor (), I +" The chat content is here .. "); 50.} 51. 52.// Send File effect 1, ring progress bar, which is also the default Effect of progressbar 53. setsendfile (layout, This , Getcurrcolor ()," My photo .jpg "); 54. 55. // Send File effect 2. The progress bar of the moment is also set to style = "? Android: ATTR/progressbarstylehorizontal "Effect 56. setsendfile2 (layout, This , Getcurrcolor ()," My photo .jpg "); 57. 58. For ( Int I = 0; I <10; I ++) 59. {60. setsendmsg (layout, This , Getcurrcolor (), I +" The chat content is here .. "); 61.} 62. sv. addview (layout ); // Add the linear layout to scrollview 63. setcontentview (SV ); // Set the current page to scrollview 64.} 65. 66. /** 67. * obtain the background color of the current chat dialogue 68. * @ return the background color of the current chat dialogue 69 .*/ 70. Private   Int Getcurrcolor () 71. {72. Return BG [(++ bgindex) % BG. Length]; 73.} 74. 75./** 76. * dynamically Add a chat content 77. * In order to simplify programming, put a person's description and content in a textview. You can split the design document into two textviews for display and set the font to 78. * @ Param layout textview control the target layout 79 to be added. * @ Param context: the required parameter for building a view control is the environment 80 of the View control. * @ Param bgcolur textview control background color 81. * @ Param MSG textview control refers to the actual text content 82. */ 83. Private   Void Setsendmsg (linearlayout layout, context, Int Bgcolur, string MSG) 84. {85. textview TV = New Textview (context ); // Normal chat Dialog 86.// Obtain a global calendar instance, which is used to obtain the current system time and format it into hours: in the form of minutes. It is only used for testing. The time here should be provided by other programs 87. TV. settext (" Someone said :[ "+ Dateformat. Format (" KK: mm ", Calendar. getinstance () +" ] \ N "+ MSG); 88. TV. setbackgroundcolor (bgcolur); 89. layout. addview (TV); 90.} 91. 92. /** 93. * dynamically Add a session entry 94 for sending files. * As the sending progress bar and the cancel button are horizontally aligned to the method, you need to add a linearlayout 95. * @ Param layout the target layout 96 to be added. * @ Param context: the required parameter for building the view control is the view control environment 97. * @ Param bgcolur control background color 98. * @ Param MSG control requires the actual text content 99. */ 100. Private  Void Setsendfile (linearlayout layout, context, Int Bgcolur, string filename) 101. {102. // Tell someone [time] 103. // All the file information to be sent must be drawn by setsendmsg! 104. setsendmsg (layout, context, bgcolur ," Sending "+ Filename); 105. // Horizontal arrangement two controls require a linearlayout. The default arrangement mode is horizontal arrangement. 106. linearlayout mylayout = New Linearlayout (context); 107. // You need to set the background color of the linearlayout control. Otherwise, the color of the main linearlayout is displayed, that is, 0xff00ffff. 108. mylayout. setbackgroundcolor (bgcolur); 109. 110. // Dynamically create a progressbar to add the default attribute to mylayout 111. progressbar Pb = New Progressbar (context); 112. Pb. setlayoutparams (lp_ww); 113. mylayout. addview (PB); 114. 115. // Dynamically create a button to add the default attribute to mylayout 116. Button bt = New Button (context); 117. bt. setlayoutparams (lp_ww); 118. bt. settext (" Cancel "); 119. mylayout. addview (BT); 120. // Add the horizontal layout linearlayout and all its controls to the master layout. 121. layout. addview (mylayout); 122.} 123. 124. /** 125. * dynamically add 126 session entries for a file to be sent. * To ensure that the background color of the progressbar and button meets the design requirements, a linearlayout is added and the background color is set to 127. * @ Param layout the target layout 128 to be added. * @ Param context: the required parameter for building a view control is the environment 129 of the View control. * @ Param bgcolur control background color 130. * @ Param MSG control requires the actual text content 131. */ 132. Private   Void Setsendfile2 (linearlayout layout, context, Int Bgcolur, string filename) 133. {134. setsendmsg (layout, context, bgcolur ," Sending "+ Filename); 135. 136. linearlayout mylayout = New Linearlayout (context); 137. mylayout. setbackgroundcolor (bgcolur); 138. mylayout. setorientation (linearlayout. Vertical ); // The control is vertical, and the default value is horizontal. 139. 140. // The default ssbar style is the ring type. Here, set her style to horizontal (horizontal line) 141. Pb = New Progressbar (context, Null , Android. R. ATTR. progressbarstylehorizontal); 142. Pb. setlayoutparams (lp_fw); 143. Pb. setprogress (45 ); // Set 1st progress to 45 144. Pb. setsecondaryprogress (0 ); // Here we do not need the 2nd Progress, so it is 0 145. mylayout. addview (PB); 146. 147. Button bt =New Button (context); 148. bt. setlayoutparams (lp_ww); 149. bt. settext (" Cancel "); 150. mylayout. addview (BT); 151. 152. layout. addview (mylayout); 153.} 154. 155. @ override 156. Public   Boolean Onkeydown ( Int Keycode, keyevent event) 157. {158. log. D (" Onkeydown: "," Keycode = "+ Keycode +" Keyevent = "+ Event); 159. Switch (Keycode) 160. {161. Case Keyevent. keycode_dpad_up: 162. 163. Break ; 164. Case Keyevent. keycode_dpad_down: 165. 166. Break ; 167. Case Keyevent. keycode_dpad_left: 168. // You can press the left button on the right to control the increase or decrease of the First progress. 169. Pb. setprogress (PB. getprogress ()-5); 170. Break ; 171. Case Keyevent. keycode_dpad_right: 172. Pb. setprogress (PB. getprogress () + 5); 173. Break ; 174. Case Keyevent. keycode_dpad_center: 175. 176. Break ; 177. Case Keyevent. keycode_0: 178. Break 179.} 180. Return   Super . Onkeydown (keycode, event); 181.} 182 .}
 
(Original address: http://www.javaeye.com/topic/717932) 
Technorati Tag: Android, dynamic, control
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.