Android custom Dynamic layout-multi-image upload
This article describes the dynamic layout of Android to add images and upload multiple images.
In the project:
Technical points:
1. dynamically add a line in the landscape and add a picture to the + sign
2. Upload Multiple images asynchronously
First look at the layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:background= "#f2f2f2" > <linearlayout android:id= "@+id/layo Ut_content "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:backgrou Nd= "#f2f2f2" android:orientation= "vertical" android:padding= "5DP" > <!--layout is dynamically generated by the program- <linearlayout android:id= "@+id/layout_container" android:layout_width= "300DP" Androi d:layout_height= "Wrap_content" android:layout_gravity= "Center_horizontal" android:layout_margin= "5DP "Android:background=" #cbcbcb "android:orientation=" vertical "android:padding=" 0.2px "/&G T <textview android:id= "@+id/text_no_data" android:layout_width= "Wrap_content" android:l ayout_height= "Wrap_content "android:layout_margin=" 20DP "android:text=" @string/text_picture_upload "Android : textsize= "16DP"/> </LinearLayout></LinearLayout>
The layout is simple, primarily a linearlayout with ID Layout_container as the parent layout.
Horizontal lines and vertical line layouts are simple:
<view xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "1px" android:background= "#cbcbcb"/>
<view xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "1px" android: layout_height= "Match_parent" android:background= "#cbcbcb"/>
Here's how the dynamically generated layouts are implemented:
private void Initui () {Setcontentview (r.layout.activity_main); Settitle (r.string.button_service_upload_picture); Showbackwardview (R.string.button_backward, true); Showforwardview (r.string.button_upload,true); Top-level parent Layout mlayout = (viewgroup) Findviewbyid (R.id.layout_container); Final int count = 9; 9 lattice Final int rowCount = (count + 2)/3; for (int i = 0; i < RowCount; i++) {if (I! = 0) {//Load Landscape layout line view.inflate (thi S, r.layout.layout_line_horizonal, mlayout); }//Create layout object, set Press color final LinearLayout linearlayout = new LinearLayout (this); Linearlayout.setbackgroundresource (R.drawable.row_selector); for (int j = 0; J < 3; J + +) {if (J! = 0) {//load inner layer vertical layout line VIEW.INFL Ate (this, r.layout.layout_line_vertical, linearlayout); } ImageButton ImageButton = new ImageButton (this); Imagebutton.setbackgroundresource (R.drawable.row_selector); Imagebutton.settag (TAG); Imagebutton.setonclicklistener (this); Imagebutton.setenabled (FALSE); Linearlayout.layoutparams layoutparams = new Linearlayout.layoutparams (layoutparams.match_parent, L Ayoutparams.match_parent, 1.0f); Add to LinearLayout layout Linearlayout.addview (ImageButton, layoutparams); Add the ImageButton object to the list Mimagebuttonlist.add (ImageButton); } Displaymanager Manager = Displaymanager.getinstance (); Layoutparams layoutparams = new Layoutparams (layoutparams.match_parent, Manager.diptopixel (100)); Add view to the total parent layout Mlayout.addview (LinearLayout, layoutparams); }//Outer set ImageButton Property final ImageButton Currentimagebutton = Mimagebuttonlist.get (mcurrent); CurrenTimagebutton.setimageresource (r.drawable.ic_add_picture); Currentimagebutton.setscaletype (Scaletype.center); Currentimagebutton.setenabled (TRUE); }
Image upload function:
Private class Uploadpicturetask extends Asynctask<list<string>, Integer, string> {/* (Non-javadoc) * @see Android.os.asynctask#doinbackground (params[]) */@Override protected String Doinbackgro und (List<string>. params) {final list<string> picturelist = params[0]; for (int i = 0, Len = picturelist.size (), i < Len; i++) {final file File = new file (Picturelist.get (i)) ; Final String response = apachehttputils.post (Murlprefix + "/upload", new file[] {File}); Parse, store//final uploadinfo upload = new Uploadparser (). Parse (response). GetData (); /*if (upload! = null) {final String url = upload.geturl (); if (URL! = null) {mpictureurllist.add (URL); }}*/publishprogress (i); } return null; } /* (non-javadoc) * @see android.os.asynctask#onprogressupdate (progress[]) */@Override P rotected void Onprogressupdate (Integer ... values) {}/* (non-javadoc) * @see Android.os.asynctask#o Npostexecute (java.lang.Object) */@Override protected void OnPostExecute (String result) { Addpictures (); Super.onpostexecute (result); } }
Note: Three lists are declared in the class to save records that were previously manipulated
Mimagebuttonlist = new arraylist<imagebutton> (); Mpicturepathlist = new arraylist<string> (); Mpictureurllist = new arraylist<string> ();
about the details of everyone interested in the download source learning it.
Welcome to Download Source:http://download.csdn.net/download/gao_chun/8776533
Reprint please specify.
Android custom Dynamic layout-multi-image upload