android简单进度条

冷不防 2022-09-29 06:14 286阅读 0赞

效果:

Center

一:布局文件

Center 1

activity_main.xml文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical" >
  5. <!-- 大环形进度条 -->
  6. <TextView
  7. android:id="@+id/textView1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="一:大环形进度条"
  11. android:textSize="25sp" />
  12. <ProgressBar
  13. style="?android:attr/progressBarStyleLarge"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:progressDrawable="@drawable/myprogressbarstyle" />
  17. <!-- 小环形进度条 -->
  18. <TextView
  19. android:id="@+id/textView2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="一:小环形进度条"
  23. android:textSize="25sp" />
  24. <ProgressBar
  25. style="?android:attr/progressBarStyleSmall"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content" />
  28. <!-- 水平进度条 -->
  29. <TextView
  30. android:id="@+id/textView3"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="一:水平进度条"
  34. android:textSize="25sp" />
  35. <!-- indeterminate是否不精确显示 -->
  36. <!-- max进度条总的范围 -->
  37. <!-- progress第一进度 -->
  38. <!-- secondaryProgress第二进度 -->
  39. <!-- progressDrawable进度条样式 -->
  40. <ProgressBar
  41. android:id="@+id/myprogressbar"
  42. style="?android:attr/progressBarStyleHorizontal"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:layout_marginLeft="3dp"
  46. android:layout_marginRight="3dp"
  47. android:indeterminate="false"
  48. android:max="100"
  49. android:progress="40"
  50. android:progressDrawable="@drawable/myprogressbarstyle"
  51. android:secondaryProgress="60" />
  52. <Button
  53. android:id="@+id/btn_progressbar"
  54. android:layout_width="match_parent"
  55. android:layout_height="wrap_content"
  56. android:text="进度条前进" />
  57. <Button
  58. android:id="@+id/btn_progressbardialog"
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:text="对话框式进度条" />
  62. </LinearLayout>

注意: android:progressDrawable=”@drawable/myprogressbarstyle”须:

Center 2

myprogressbarstyle.xml文件:(可以自行修改)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!-- centerColor背景色 -->
  4. <!-- startColor顶部线条颜色 -->
  5. <!-- endColor底部线条颜色 -->
  6. <item android:id="@android:id/background">
  7. <shape>
  8. <corners android:radius="5dip" />
  9. <gradient
  10. android:angle="270"
  11. android:centerColor="#bfbfbf"
  12. android:centerY="0.75"
  13. android:endColor="#bfbfbf"
  14. android:startColor="#bfbfbf" />
  15. </shape>
  16. </item>
  17. <item android:id="@android:id/secondaryProgress">
  18. <clip>
  19. <shape>
  20. <corners android:radius="5dip" />
  21. <gradient
  22. android:angle="270"
  23. android:centerColor="#00ffff"
  24. android:centerY="0.75"
  25. android:endColor="#00ffff"
  26. android:startColor="#00ffff" />
  27. </shape>
  28. </clip>
  29. </item>
  30. <item android:id="@android:id/progress">
  31. <clip>
  32. <shape>
  33. <corners android:radius="5dip" />
  34. <gradient
  35. android:angle="270"
  36. android:centerColor="#0000ff"
  37. android:centerY="0.75"
  38. android:endColor="#0000ff"
  39. android:startColor="#0000ff" />
  40. </shape>
  41. </clip>
  42. </item>
  43. </layer-list>

二:src

Center 3

MainActivity.java文件:

  1. package com.peng.butongprograssbar;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.view.Window;
  6. import android.widget.Button;
  7. import android.widget.ProgressBar;
  8. import android.widget.Toast;
  9. import android.app.Activity;
  10. import android.app.ProgressDialog;
  11. import android.content.DialogInterface;
  12. public class MainActivity extends Activity implements OnClickListener {
  13. // 声明控件
  14. private Button btn_progressbar;
  15. private Button btn_progressbardialog;
  16. private ProgressBar myprogressbar;
  17. private ProgressDialog showDialog;
  18. // 数据
  19. int firstprogress;// 第一进度
  20. int secondprogress;// 第二进度
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. // 标题栏中的进度条
  25. requestWindowFeature(Window.FEATURE_PROGRESS);// 水平式进度条
  26. requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);// 圆圈式进度条(默认进度条)
  27. setContentView(R.layout.activity_main);
  28. // 显示标题栏中的进度条
  29. setProgressBarVisibility(true);
  30. setProgressBarIndeterminateVisibility(true);
  31. // setProgress(10000);//注意:这里默认水平进度条的最大值为10000,设置为10000出现后马上消失
  32. setProgress(9999);
  33. // 获取控件
  34. initViews();
  35. // 初始化数据
  36. initDatas();
  37. // 为控件添加事件
  38. setViewsListener();
  39. }
  40. private void initDatas() {
  41. firstprogress = myprogressbar.getProgress();// 第一进度
  42. secondprogress = myprogressbar.getSecondaryProgress();// 第二进度
  43. }
  44. private void setViewsListener() {
  45. btn_progressbar.setOnClickListener(this);
  46. btn_progressbardialog.setOnClickListener(this);
  47. }
  48. private void initViews() {
  49. btn_progressbar = (Button) findViewById(R.id.btn_progressbar);
  50. btn_progressbardialog = (Button) findViewById(R.id.btn_progressbardialog);
  51. myprogressbar = (ProgressBar) findViewById(R.id.myprogressbar);
  52. }
  53. // 监听事件--处理
  54. @Override
  55. public void onClick(View v) {
  56. switch (v.getId()) {
  57. case R.id.btn_progressbar: {
  58. if (100 != secondprogress) {
  59. myprogressbar.setProgress(firstprogress++);
  60. myprogressbar.setSecondaryProgress(secondprogress++);
  61. } else {
  62. if (100 != firstprogress) {
  63. myprogressbar.setProgress(firstprogress++);
  64. } else {
  65. Toast.makeText(MainActivity.this, "全部加载完成1", 0).show();
  66. }
  67. }
  68. break;
  69. }
  70. case R.id.btn_progressbardialog: {
  71. showDialog = new ProgressDialog(MainActivity.this);
  72. showDialog.setTitle("提示");
  73. showDialog.setIcon(R.drawable.ic_launcher);
  74. showDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  75. showDialog.setMessage("正在加载中...");
  76. showDialog.setMax(100);
  77. showDialog.incrementProgressBy(20);
  78. showDialog.setIndeterminate(false);
  79. showDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
  80. new DialogInterface.OnClickListener() {
  81. @Override
  82. public void onClick(DialogInterface dialog, int which) {
  83. Toast.makeText(MainActivity.this, "exit!",
  84. Toast.LENGTH_SHORT).show();
  85. }
  86. });
  87. showDialog.setCancelable(true);// 物理返回按键退出对话框
  88. showDialog.show();
  89. break;
  90. }
  91. }
  92. }
  93. }

发表评论

表情:
评论列表 (有 0 条评论,286人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android进度

    ProgressBar:进度条。系统提供四种类型进度条,大、中、小圆形进度条和条形进度条 更换成条形进度条类型的代码: ![Center][] 实现进度条还需在ja