<?xml version="1.0"encoding="UTF-8"? ><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--define the background of the track--<item android:id="@android: Id/background"android:drawable="@drawable/ok"/> <!--define the finished part of the track style-<item android:id="@android: Id/progress"android:drawable="@drawable/no"/></layer-list>
My_bar.xml//Configuration progress bar picture
@drawable/ok @drawable/no to save the picture in the Drawable folder
Progress bar configuration in Layout files
<progressbar <!--Use the system default-->android:id= "@+id/bar" android:layout_width= "Fill_parent" Android:layout_ height= "Wrap_content" android:max= "style=" @android: Style/widget.progressbar.horizontal "/><!-- Define a horizontal progress bar and change the track appearance--><progressbar <!--custom-->android:id= "@+id/bar2" android:layout_width= "Fill_ Parent "android:layout_height=" wrap_content "android:max=" android:progressdrawable= "@drawable/my_bar" <!--This is the configuration file above-->style= "@android: Style/widget.progressbar.horizontal"/>
Call in activity
1 Public classProgressbartest extends Activity2 {3 //The program simulates populating an array with a length of 1004 Private int[] data =New int[ -];5 intHasData =0;6 //record the completion progress of ProgressBar7 intStatus =0; 8 @Override9 Public voidonCreate (Bundle savedinstancestate)Ten { One super.oncreate (savedinstancestate); A Setcontentview (r.layout.main); -Final ProgressBar bar =(ProgressBar) Findviewbyid (R.id.bar); -Final ProgressBar bar2 =(ProgressBar) Findviewbyid (R.ID.BAR2); the //create a handler that is responsible for the progress of the update -Final Handler Mhandler =NewHandler () - { - @Override + Public voidhandlemessage (Message msg) - { + //indicates that the message was sent by the program. A if(Msg.what = =0x111) at { - bar.setprogress (status); - bar2.setprogress (status); - } - } - }; in //to start a thread to perform a task - NewThread () to { + Public voidRun () - { the while(Status < -) * { $ //get percent complete for time-consuming operationsPanax NotoginsengStatus =doWork (); - //send message to Handler theMessage m =NewMessage (); +M.what =0x111; A //Send Message the Mhandler.sendmessage (m); + } - } $ }.start (); $ } - //simulates a time-consuming operation. - Public intdoWork () the { - //Assigning a value to an array elementWuyidata[hasdata++] = (int) (Math.random () * -); the Try - { WuThread.Sleep ( -); - } About Catch(interruptedexception e) $ { - e.printstacktrace (); - } - returnHasData; A } +}
Android ProgressBar level progress bar