三种进度条方式:滚动进度条,水平进度条,自定义进度条
废话不多说,直接上代码。
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <!--滚动等待--> <Button android:id="@+id/bu_circle_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="滚动等待进度条"/> <!--水平进度条加载--> <Button android:id="@+id/bu_horizontal_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="水平进度条"/> <!--自定义加载提示框--> <Button android:id="@+id/bu_my_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义提示框"/> </LinearLayout>
java:
public class ProgressDemoActivity extends AppCompatActivity implements View.OnClickListener { //滚动等待 Button circle_progress; //水平进度条 Button horizontal_progress; //自定义加载框 Button my_progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress_demo); circle_progress=findViewById(R.id.bu_circle_progress); horizontal_progress=findViewById(R.id.bu_horizontal_progress); my_progress=findViewById(R.id.bu_my_progress); } public void onClick(View v) { switch (v.getId()){ case R.id.bu_circle_progress: final ProgressDialog progressDialog= new ProgressDialog(ProgressDemoActivity.this); //设定标题图片 progressDialog.setIcon(R.drawable.tip1); //设定标题 progressDialog.setTitle("请等待"); //设定内容展示 progressDialog.setMessage("加载中。。。"); //设置不可取消 progressDialog.setCancelable(false); //不要忘记展示进度条 progressDialog.show(); //模拟耗时操作 new Thread(new Runnable() { @Override public void run() { try { //等待5秒 Thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); }finally { //销毁进度条 progressDialog.dismiss(); } } }).start(); break; case R.id.bu_horizontal_progress: final ProgressDialog progressDialog1= new ProgressDialog(ProgressDemoActivity.this); //设定进度条展示为水平 progressDialog1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog1.setTitle("提示"); progressDialog1.setMessage("加载中..."); progressDialog1.setIcon(R.drawable.tip1); //设置最大值 progressDialog1.setMax(100); //设定进度条默认进度 progressDialog1.setProgress(0); progressDialog1.setCancelable(false); progressDialog1.show(); new Thread(new Runnable() { @Override public void run() { int count=0; while (count<=100) { progressDialog1.setProgress(count++); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } progressDialog1.dismiss(); } }).start(); break; case R.id.bu_my_progress: final Dialog dialog= DialogUtil.createLoadingDialog(this,"加载中请等待。。。"); dialog.show(); //模拟耗时任务 new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); }finally { dialog.dismiss(); } } }).start(); }
自定义的布局需要在layout中根据自己需求来设计
效果展示:
滚动进度条:
水平进度条:
自定义进度条: