<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.example.progressbar.mainactivity$placeholderfragment" > <LinearLayoutandroid:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"> <!--define a large circular progress bar - <ProgressBarstyle= "? Android:attr/progressbarstylelarge"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <!--define a normal circular progress bar - <ProgressBarAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <!--Define a small loop progress bar - <ProgressBarstyle= "? Android:attr/progressbarstylesmall"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /></LinearLayout> <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/progress" /> <!--Horizontal progress bar - <ProgressBarAndroid:id= "@+id/bar1"style= "? Android:attr/progressbarstylehorizontal"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:max= "+" /> <!--Customizing the horizontal progress bar - <ProgressBarAndroid:id= "@+id/bar"style= "? Android:attr/progressbarstylehorizontal"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:progressdrawable= "@drawable/my_bar"Android:max= "+" /></LinearLayout>
<?XML version= "1.0" encoding= "Utf-8"?><layer-listxmlns:android= "Http://schemas.android.com/apk/res/android" > <!--defining the track background - <ItemAndroid:id= "@android: Id/background"android:drawable= "@drawable/no"></Item><ItemAndroid:id= "@android: id/progress"android:drawable= "@drawable/ok"></Item></layer-list>
PackageCom.example.progressbar;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.widget.ProgressBar; Public classMainactivityextendsActivity {Private int[] Data=New int[100]; intHasdata=0; //Record Completion Progress intStatus=0; ProgressBar bar,bar1; //create a handler that is responsible for updating progressHandler mhandler=NewHandler () { Public voidhandlemessage (Message msg) {if(msg.what==0x111) {bar.setprogress (status); Bar1.setprogress (status); } } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Bar=(ProgressBar) Findviewbyid (R.id.bar); Bar1=(ProgressBar) Findviewbyid (R.ID.BAR1); NewThread () { Public voidrun () { while(status<100){ //get percent complete for time-consuming operationsstatus=doWork (); //Send MessageMhandler.sendemptymessage (0x111); }}}.start (); } Public intdoWork () {Data[hasdata++]=(int) (Math.random () *100); Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); } returnHasData; }}
Only in the use of the thread, handler knowledge.
Android 20th Lesson--progressbar